<?php

namespace App\Http\Controllers\Care;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Models\Bed;
use App\Models\CareUnit;
use App\Services\Care\AuditLogger;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class BedController extends Controller
{
    use ScopesToAccount;

    public function store(Request $request, CareUnit $careUnit): RedirectResponse
    {
        $this->authorizeAbility($request, 'admin.departments.manage');
        $this->authorizeOwner($request, $careUnit);
        abort_unless($careUnit->supportsBeds(), 422, 'Beds are only for inpatient care units.');

        $validated = $request->validate([
            'label' => ['required', 'string', 'max:100'],
            'room' => ['nullable', 'string', 'max:100'],
            'status' => ['required', 'string', Rule::in(array_keys(config('care.bed_statuses')))],
        ]);

        $bed = Bed::create([
            'owner_ref' => $this->ownerRef($request),
            'organization_id' => $careUnit->organization_id,
            'care_unit_id' => $careUnit->id,
            'label' => $validated['label'],
            'room' => $validated['room'] ?? null,
            'status' => $validated['status'],
            'is_active' => true,
        ]);

        AuditLogger::record(
            $this->ownerRef($request),
            'bed.created',
            $careUnit->organization_id,
            $this->ownerRef($request),
            Bed::class,
            $bed->id,
        );

        return redirect()->route('care.care-units.show', $careUnit)->with('success', 'Bed added.');
    }

    public function update(Request $request, CareUnit $careUnit, Bed $bed): RedirectResponse
    {
        $this->authorizeAbility($request, 'admin.departments.manage');
        $this->authorizeOwner($request, $careUnit);
        abort_unless((int) $bed->care_unit_id === (int) $careUnit->id, 404);
        $this->authorizeOwner($request, $bed);

        $validated = $request->validate([
            'label' => ['required', 'string', 'max:100'],
            'room' => ['nullable', 'string', 'max:100'],
            'status' => ['required', 'string', Rule::in(array_keys(config('care.bed_statuses')))],
            'is_active' => ['boolean'],
        ]);

        $bed->update([
            'label' => $validated['label'],
            'room' => $validated['room'] ?? null,
            'status' => $validated['status'],
            'is_active' => $request->boolean('is_active', true),
        ]);

        AuditLogger::record(
            $this->ownerRef($request),
            'bed.updated',
            $careUnit->organization_id,
            $this->ownerRef($request),
            Bed::class,
            $bed->id,
        );

        return redirect()->route('care.care-units.show', $careUnit)->with('success', 'Bed updated.');
    }

    public function destroy(Request $request, CareUnit $careUnit, Bed $bed): RedirectResponse
    {
        $this->authorizeAbility($request, 'admin.departments.manage');
        $this->authorizeOwner($request, $careUnit);
        abort_unless((int) $bed->care_unit_id === (int) $careUnit->id, 404);
        $this->authorizeOwner($request, $bed);

        $bed->delete();

        AuditLogger::record(
            $this->ownerRef($request),
            'bed.deleted',
            $careUnit->organization_id,
            $this->ownerRef($request),
            Bed::class,
            $bed->id,
        );

        return redirect()->route('care.care-units.show', $careUnit)->with('success', 'Bed removed.');
    }
}
