<?php

namespace Modules\MedEmergency\Http\Controllers;

use App\Http\Controllers\Controller;

/**
 * Shared helpers for the MedEmergency back-office controllers: business scoping,
 * permission guards and the standard JSON response envelope. Mirrors
 * Modules\MedBilling\Http\Controllers\MedBillingBaseController.
 */
abstract class MedEmergencyBaseController extends Controller
{
    protected function businessId(): int
    {
        return (int) request()->session()->get('user.business_id');
    }

    protected function userId(): ?int
    {
        return request()->session()->get('user.id');
    }

    protected function guard(string $permission): void
    {
        if (! auth()->user()->can($permission)) {
            abort(403, 'Unauthorized action.');
        }
    }

    protected function ok(string $msg, array $extra = []): array
    {
        return array_merge(['success' => true, 'msg' => $msg], $extra);
    }

    protected function fail(?string $msg = null): array
    {
        return ['success' => false, 'msg' => $msg ?: __('messages.something_went_wrong')];
    }
}
