<?php

namespace App\Policies;

use App\Models\Payment;
use App\Models\User;

class PaymentPolicy
{
    /**
     * Determine if the user can view any payments.
     */
    public function viewAny(User $user): bool
    {
        return $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can view the payment.
     */
    public function view(User $user, Payment $payment): bool
    {
        // Admin and staff can view all
        if ($user->hasRole('admin') || $user->hasRole('staff')) {
            return true;
        }

        // Customers can only view their own payments
        if ($user->hasRole('customer')) {
            return $payment->customer_id === $user->id;
        }

        return false;
    }

    /**
     * Determine if the user can create payments.
     */
    public function create(User $user): bool
    {
        return $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can update the payment.
     */
    public function update(User $user, Payment $payment): bool
    {
        return $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can delete the payment.
     */
    public function delete(User $user, Payment $payment): bool
    {
        return $user->hasRole('admin');
    }

    /**
     * Determine if the user can refund the payment.
     */
    public function refund(User $user, Payment $payment): bool
    {
        return $user->hasRole('admin');
    }
}
