<?php

namespace App\Policies;

use App\Models\Quotation;
use App\Models\User;

class QuotationPolicy
{
    /**
     * Determine if the user can view any quotations.
     */
    public function viewAny(User $user): bool
    {
        // Load role relationship if not loaded
        if (! $user->relationLoaded('role')) {
            $user->load('role');
        }

        return $user->hasRole('super-admin') || $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can view the quotation.
     */
    public function view(User $user, Quotation $quotation): bool
    {
        // Load role relationship if not loaded
        if (! $user->relationLoaded('role')) {
            $user->load('role');
        }

        // Super admin, admin and staff can view all
        if ($user->hasRole('super-admin') || $user->hasRole('admin') || $user->hasRole('staff')) {
            return true;
        }

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

        return false;
    }

    /**
     * Determine if the user can create quotations.
     */
    public function create(User $user): bool
    {
        // Load role relationship if not loaded
        if (! $user->relationLoaded('role')) {
            $user->load('role');
        }

        return $user->hasRole('super-admin') || $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can update the quotation.
     */
    public function update(User $user, Quotation $quotation): bool
    {
        // Load role relationship if not loaded
        if (! $user->relationLoaded('role')) {
            $user->load('role');
        }

        return $user->hasRole('super-admin') || $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can delete the quotation.
     */
    public function delete(User $user, Quotation $quotation): bool
    {
        // Load role relationship if not loaded
        if (! $user->relationLoaded('role')) {
            $user->load('role');
        }

        return $user->hasRole('super-admin') || $user->hasRole('admin');
    }

    /**
     * Determine if the user can convert quotation to invoice.
     */
    public function convertToInvoice(User $user, Quotation $quotation): bool
    {
        // Load role relationship if not loaded
        if (! $user->relationLoaded('role')) {
            $user->load('role');
        }

        return $user->hasRole('super-admin') || $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can send the quotation (email / WhatsApp).
     */
    public function send(User $user, Quotation $quotation): bool
    {
        return $user->hasRole('super-admin') || $user->hasRole('admin') || $user->hasRole('staff');
    }

    /**
     * Determine if the user can download the quotation PDF.
     */
    public function downloadPdf(User $user, Quotation $quotation): bool
    {
        return $this->view($user, $quotation);
    }
}
