<?php

namespace App\Policies;

use App\Models\LoanRestructuring;
use App\Models\User;
use App\Services\BranchAccessService;
use App\Support\Authorization\PermissionName;

class LoanRestructuringPolicy
{
    public function __construct(private BranchAccessService $branchAccess)
    {
    }

    public function view(User $user, LoanRestructuring $restructuring): bool
    {
        return $user->can(PermissionName::LOANS_ADJUSTMENTS_VIEW->value)
            && $this->canAccess($user, $restructuring);
    }

    public function decide(User $user, LoanRestructuring $restructuring): bool
    {
        return $user->can(PermissionName::LOANS_RESTRUCTURE_APPROVE->value)
            && $this->canAccess($user, $restructuring);
    }

    public function downloadDocument(User $user, LoanRestructuring $restructuring): bool
    {
        return $this->view($user, $restructuring);
    }

    private function canAccess(User $user, LoanRestructuring $restructuring): bool
    {
        return $this->branchAccess->canAccessBranch($user, (int) $restructuring->branch_id);
    }
}
