<?php

namespace App\Modules\HR\Loans\Policies;

use App\Models\User;
use App\Modules\HR\Loans\Models\Loan;

class LoanPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->can('loans.view');
    }

    public function view(User $user, Loan $loan): bool
    {
        return $this->sameTenant($user, $loan) && $user->can('loans.view');
    }

    public function create(User $user): bool
    {
        return $user->can('loans.create');
    }

    public function update(User $user, Loan $loan): bool
    {
        return $this->sameTenant($user, $loan) && $user->can('loans.update');
    }

    public function approve(User $user, Loan $loan): bool
    {
        return $this->sameTenant($user, $loan) && $user->can('loans.approve');
    }

    public function manage(User $user, Loan $loan): bool
    {
        return $this->sameTenant($user, $loan) && $user->can('loans.manage');
    }

    private function sameTenant(User $user, Loan $loan): bool
    {
        return (int) $user->tenant_id === (int) $loan->tenant_id;
    }
}
