<?php

namespace App\Livewire\JobCards;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\JobCard;
use App\Models\Customer;
use App\Models\Vehicle;

class Index extends Component
{
    use WithPagination;

    public $search = '';
    public $statusFilter = '';
    public $branchFilter = '';
    public $priorityFilter = '';
    public $sortBy = 'created_at';
    public $sortDirection = 'desc';

    protected $queryString = [
        'search' => ['except' => ''],
        'statusFilter' => ['except' => ''],
        'branchFilter' => ['except' => ''],
        'priorityFilter' => ['except' => ''],
        'sortBy' => ['except' => 'created_at'],
        'sortDirection' => ['except' => 'desc'],
    ];

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function updatingStatusFilter()
    {
        $this->resetPage();
    }

    public function updatingBranchFilter()
    {
        $this->resetPage();
    }

    public function updatingPriorityFilter()
    {
        $this->resetPage();
    }

    public function sortBy($field)
    {
        if ($this->sortBy === $field) {
            $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
        } else {
            $this->sortBy = $field;
            $this->sortDirection = 'asc';
        }
    }

    public function render()
    {
        $jobCards = JobCard::query()
            ->with(['customer', 'vehicle', 'serviceAdvisor'])
            ->when($this->search, function ($query) {
                $query->where(function ($q) {
                    $q->where('job_card_number', 'like', '%' . $this->search . '%')
                      ->orWhereHas('customer', function ($customerQuery) {
                          $customerQuery->where('first_name', 'like', '%' . $this->search . '%')
                                      ->orWhere('last_name', 'like', '%' . $this->search . '%')
                                      ->orWhere('email', 'like', '%' . $this->search . '%');
                      })
                      ->orWhereHas('vehicle', function ($vehicleQuery) {
                          $vehicleQuery->where('license_plate', 'like', '%' . $this->search . '%')
                                     ->orWhere('vin', 'like', '%' . $this->search . '%');
                      });
                });
            })
            ->when($this->statusFilter, function ($query) {
                $query->where('status', $this->statusFilter);
            })
            ->when($this->branchFilter, function ($query) {
                $query->where('branch_code', $this->branchFilter);
            })
            ->when($this->priorityFilter, function ($query) {
                $query->where('priority', $this->priorityFilter);
            })
            ->orderBy($this->sortBy, $this->sortDirection)
            ->paginate(20);

        $statusOptions = [
            'received' => 'Received',
            'in_diagnosis' => 'In Diagnosis',
            'estimate_sent' => 'Estimate Sent',
            'approved' => 'Approved',
            'in_progress' => 'In Progress',
            'quality_check' => 'Quality Check',
            'completed' => 'Completed',
            'delivered' => 'Delivered',
            'cancelled' => 'Cancelled',
        ];

        $priorityOptions = [
            'low' => 'Low',
            'medium' => 'Medium',
            'high' => 'High',
            'urgent' => 'Urgent',
        ];

        $branchOptions = [
            'ACC' => 'ACC Branch',
            'KSI' => 'KSI Branch',
            // Add more branches as needed
        ];

        return view('livewire.job-cards.index', compact('jobCards', 'statusOptions', 'priorityOptions', 'branchOptions'));
    }
}
