<?php

namespace App\Policies;

use App\Models\Estimate;
use App\Models\User;

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

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

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

        return false;
    }

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

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

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

    /**
     * Determine if the user can convert estimate to invoice.
     */
    public function convertToInvoice(User $user, Estimate $estimate): bool
    {
        return $user->hasRole('admin') || $user->hasRole('staff');
    }
}
