<?php

namespace App\Policies;

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

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

    public function view(User $user, PayrollPeriod $period): bool
    {
        return $user->can(PermissionName::PAYROLL_VIEW->value)
            && $this->branchAccess->canAccessBranch($user, (int) $period->branch_id);
    }

    public function updateEntries(User $user, PayrollPeriod $period): bool
    {
        return $user->can(PermissionName::PAYROLL_PERIODS_CREATE->value)
            && $this->branchAccess->canAccessBranch($user, (int) $period->branch_id);
    }

    public function decide(User $user, PayrollPeriod $period): bool
    {
        return $user->can(PermissionName::PAYROLL_APPROVE->value)
            && $this->branchAccess->canAccessBranch($user, (int) $period->branch_id);
    }

    public function pay(User $user, PayrollPeriod $period): bool
    {
        return $user->can(PermissionName::PAYROLL_PAY->value)
            && $this->branchAccess->canAccessBranch($user, (int) $period->branch_id);
    }
}
