<?php

namespace App\Http\Controllers\Care;

use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Http\Controllers\Controller;
use App\Services\Care\BloodBank\BloodBankAnalyticsService;
use App\Services\Care\BloodBank\BloodBankInventoryService;
use App\Services\Care\BranchContext;
use App\Services\Care\CarePermissions;
use App\Services\Care\SpecialtyModuleService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class BloodBankAdminController extends Controller
{
    use ScopesToAccount;

    public function __construct(
        protected BloodBankInventoryService $inventory,
        protected BloodBankAnalyticsService $analytics,
        protected BranchContext $branchContext,
    ) {}

    public function index(
        Request $request,
        SpecialtyModuleService $modules,
    ): View {
        $this->authorizeAbility($request, 'blood_bank.manage');
        $organization = $this->organization($request);
        abort_unless($modules->isEnabled($organization, 'blood_bank'), 404);

        $member = $this->member($request);
        $branches = $this->branchContext->branches($request, $organization, $member);
        $branchId = $this->branchContext->resolve($request, $organization, $member);
        $branchScope = $branchId > 0 ? $branchId : null;

        $overview = $this->inventory->adminOverview(
            $organization,
            $this->ownerRef($request),
            $branchScope,
        );

        $report = $this->analytics->report(
            $organization,
            $this->ownerRef($request),
            $branchScope,
        );

        return view('care.blood-bank.admin.index', [
            'organization' => $organization,
            'branches' => $branches,
            'branchId' => $branchId,
            'canSwitchBranch' => $this->branchContext->canSwitch($member),
            'overview' => $overview,
            'report' => $report,
            'isAdmin' => app(CarePermissions::class)->isAdmin($member),
        ]);
    }

    public function updateStock(
        Request $request,
        SpecialtyModuleService $modules,
    ): RedirectResponse {
        $this->authorizeAbility($request, 'blood_bank.manage');
        $organization = $this->organization($request);
        abort_unless($modules->isEnabled($organization, 'blood_bank'), 404);

        $member = $this->member($request);
        $branchId = $this->branchContext->resolve($request, $organization, $member);
        $branchScope = $branchId > 0 ? $branchId : null;

        $validated = $request->validate([
            'stock' => ['required', 'array'],
            'stock.*.product_code' => ['required', 'string', 'max:64'],
            'stock.*.units_on_hand' => ['required', 'integer', 'min:0', 'max:99999'],
            'stock.*.reorder_level' => ['required', 'integer', 'min:0', 'max:9999'],
            'stock.*.notes' => ['nullable', 'string', 'max:2000'],
        ]);

        $this->inventory->updateStock(
            $organization,
            $this->ownerRef($request),
            $branchScope,
            array_values($validated['stock']),
            $this->actorRef($request),
        );

        return redirect()
            ->route('care.blood-bank.admin.index', array_filter([
                'branch_id' => $branchId > 0 ? $branchId : null,
            ]))
            ->with('success', 'Blood Bank operational inventory updated.');
    }
}
