<?php

namespace App\Http\Controllers\Care;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Services\Care\CarePermissions;
use App\Services\Care\NursingServicesService;
use App\Services\Care\RosterService;
use App\Services\Care\SpecialtyModuleService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class NursingServicesController extends Controller
{
    use ScopesToAccount;

    public function index(
        Request $request,
        NursingServicesService $nursing,
        RosterService $roster,
        SpecialtyModuleService $modules,
    ): View {
        $this->authorizeAbility($request, 'nursing.services.view');
        $organization = $this->organization($request);
        $owner = $this->ownerRef($request);

        $nurses = $nursing->nurseRegistry($organization, $owner);
        $allocations = $nursing->activeAllocations($organization, $owner);
        $todayRoster = $roster->todayAllocation($organization, $owner);
        $units = $nursing->careUnits($organization, $owner);
        $moduleEnabled = $modules->isEnabled($organization, 'nursing_services');

        $labelMembers = $nurses
            ->merge($allocations->pluck('member')->filter())
            ->merge($todayRoster->pluck('member')->filter())
            ->unique('id')
            ->values();

        $permissions = app(CarePermissions::class);
        $member = $this->member($request);

        return view('care.nursing.services-hub', [
            'organization' => $organization,
            'nurses' => $nurses,
            'nurseLabels' => $nursing->memberLabels($labelMembers),
            'allocations' => $allocations,
            'todayRoster' => $todayRoster,
            'units' => $units,
            'moduleEnabled' => $moduleEnabled,
            'roles' => config('care.roles'),
            'canRoster' => $permissions->can($member, 'nursing.roster.manage'),
            'canViewRoster' => $permissions->can($member, 'nursing.roster.view'),
            'assignmentRoles' => config('care.staff_assignment_roles'),
        ]);
    }
}
