<?php

namespace App\Http\Controllers\Care;

use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Http\Controllers\Controller;
use App\Services\Care\CarePermissions;
use App\Services\Care\NurseStationService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class NurseStationController extends Controller
{
    use ScopesToAccount;

    public function index(Request $request, NurseStationService $station): View
    {
        $this->authorizeAbility($request, 'nursing.station.view');
        $member = $this->member($request);
        abort_unless($member, 403);

        $organization = $this->organization($request);
        $unitId = $request->filled('unit') ? (int) $request->input('unit') : null;
        $board = $station->board($organization, $member, $this->ownerRef($request), $unitId);

        $permissions = app(CarePermissions::class);

        return view('care.nursing.station', [
            'units' => $board['units'],
            'patientsByUnit' => $board['patients_by_unit'],
            'dutyByUnit' => $board['duty_by_unit'],
            'placementByUnit' => $board['placement_by_unit'],
            'selectedUnit' => $board['selected_unit'],
            'canMar' => $permissions->can($member, 'mar.view'),
            'canAdminister' => $permissions->can($member, 'mar.administer'),
            'canNotes' => $permissions->can($member, 'nursing.notes.view'),
            'canHandover' => $permissions->can($member, 'nursing.handover.view'),
            'canAssess' => $permissions->can($member, 'nursing.assessments.view'),
            'canVitals' => $permissions->can($member, 'vitals.manage'),
            'canServicesHub' => $permissions->can($member, 'nursing.services.view'),
        ]);
    }
}
