<?php

namespace App\Policies;

use App\Models\Invoice;
use App\Models\User;

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

    public function view(User $user, Invoice $invoice): bool
    {
        return $user->can('invoices.view') && $invoice->tenant_id === $user->tenant_id;
    }

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

    public function update(User $user, Invoice $invoice): bool
    {
        return $user->can('invoices.manage') && $invoice->tenant_id === $user->tenant_id;
    }

    /** Quick payment recording — receptionists hold installments.manage. */
    public function recordPayment(User $user, Invoice $invoice): bool
    {
        return ($user->can('installments.manage') || $user->can('invoices.manage'))
            && $invoice->tenant_id === $user->tenant_id;
    }
}
