<?php

namespace App\Policies;

use App\Models\JobCard;
use App\Models\User;

class JobCardPolicy
{
    /**
     * Determine whether the user can view any job cards.
     */
    public function viewAny(User $user): bool
    {
        // Super admin can view all without branch restrictions
        if ($user->hasRole('super_admin')) {
            return true;
        }
        
        return $user->hasAnyPermission([
            'job-cards.view',
            'job-cards.view-all'
        ], $user->branch_code);
    }

    /**
     * Determine whether the user can view the job card.
     */
    public function view(User $user, JobCard $jobCard): bool
    {
        // Super admin can view all without branch restrictions
        if ($user->hasRole('super_admin')) {
            return true;
        }
        
        // Admin or users with view-all permission can see any job card
        if ($user->hasPermission('job-cards.view-all', $user->branch_code)) {
            return true;
        }

        // Users can view job cards in their branch
        if ($user->hasPermission('job-cards.view', $user->branch_code) && 
            $jobCard->branch_code === $user->branch_code) {
            return true;
        }

        // Service advisors can view their own job cards
        if ($user->hasPermission('job-cards.view-own') && 
            $jobCard->service_advisor_id === $user->id) {
            return true;
        }

        return false;
    }

    /**
     * Determine whether the user can create job cards.
     */
    public function create(User $user): bool
    {
        // Super admin can create without branch restrictions
        if ($user->hasRole('super_admin')) {
            return true;
        }
        
        return $user->hasPermission('job-cards.create', $user->branch_code);
    }

    /**
     * Determine whether the user can update the job card.
     */
    public function update(User $user, JobCard $jobCard): bool
    {
        // Super admin can update all without branch restrictions
        if ($user->hasRole('super_admin')) {
            return true;
        }
        
        // Admin or users with update-all permission can update any job card
        if ($user->hasPermission('job-cards.update-all', $user->branch_code)) {
            return true;
        }

        // Users can update job cards in their branch
        if ($user->hasPermission('job-cards.update', $user->branch_code) && 
            $jobCard->branch_code === $user->branch_code) {
            return true;
        }

        // Service advisors can update their own job cards
        if ($user->hasPermission('job-cards.update-own') && 
            $jobCard->service_advisor_id === $user->id) {
            return true;
        }

        return false;
    }

    /**
     * Determine whether the user can delete the job card.
     */
    public function delete(User $user, JobCard $jobCard): bool
    {
        // Super admin can delete all without branch restrictions
        if ($user->hasRole('super_admin')) {
            return true;
        }
        
        // Only admin or users with delete permission can delete
        if ($user->hasPermission('job-cards.delete', $user->branch_code)) {
            return $jobCard->branch_code === $user->branch_code;
        }

        return false;
    }

    /**
     * Determine whether the user can restore the job card.
     */
    public function restore(User $user, JobCard $jobCard): bool
    {
        // Super admin can restore all without branch restrictions
        if ($user->hasRole('super_admin')) {
            return true;
        }
        
        return $user->hasPermission('job-cards.restore', $user->branch_code);
    }

    /**
     * Determine whether the user can permanently delete the job card.
     */
    public function forceDelete(User $user, JobCard $jobCard): bool
    {
        // Super admin can force delete all without branch restrictions
        if ($user->hasRole('super_admin')) {
            return true;
        }
        
        return $user->hasPermission('job-cards.force-delete', $user->branch_code);
    }

    /**
     * Determine whether the user can approve job cards.
     */
    public function approve(User $user, JobCard $jobCard): bool
    {
        return $user->hasPermission('job-cards.approve', $user->branch_code) &&
               $jobCard->branch_code === $user->branch_code;
    }

    /**
     * Determine whether the user can assign technicians.
     */
    public function assignTechnician(User $user, JobCard $jobCard): bool
    {
        return $user->hasPermission('job-cards.assign-technician', $user->branch_code) &&
               $jobCard->branch_code === $user->branch_code;
    }
}
