<?php

namespace App\Livewire\TechnicianManagement;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Technician;

class Index extends Component
{
    use WithPagination;
    
    public $search = '';
    public $statusFilter = '';
    public $skillFilter = '';
    public $sortBy = 'first_name';
    public $sortDirection = 'asc';
    public $selectedTechnician = null;
    public $showingDetails = false;

    protected $queryString = [
        'search' => ['except' => ''],
        'statusFilter' => ['except' => ''],
        'skillFilter' => ['except' => ''],
        'sortBy' => ['except' => 'first_name'],
        'sortDirection' => ['except' => 'asc']
    ];

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

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

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

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

    public function showDetails($technicianId)
    {
        $this->selectedTechnician = Technician::with(['skills', 'performances', 'workloads'])->find($technicianId);
        $this->showingDetails = true;
    }

    public function closeDetails()
    {
        $this->selectedTechnician = null;
        $this->showingDetails = false;
    }

    public function getTechniciansProperty()
    {
        return Technician::query()
            ->when($this->search, function ($query) {
                $query->where(function ($q) {
                    $q->where('first_name', 'like', '%' . $this->search . '%')
                      ->orWhere('last_name', 'like', '%' . $this->search . '%')
                      ->orWhere('email', 'like', '%' . $this->search . '%')
                      ->orWhere('employee_id', 'like', '%' . $this->search . '%');
                });
            })
            ->when($this->statusFilter, function ($query) {
                $query->where('status', $this->statusFilter);
            })
            ->when($this->skillFilter, function ($query) {
                $query->whereHas('skills', function ($q) {
                    $q->where('skill_name', $this->skillFilter);
                });
            })
            ->with(['skills' => function($query) {
                $query->orderBy('is_primary_skill', 'desc');
            }, 'performances', 'workloads'])
            ->orderBy($this->sortBy, $this->sortDirection)
            ->paginate(10);
    }

    public function getAvailableSkillsProperty()
    {
        return \App\Models\TechnicianSkill::distinct('skill_name')
            ->pluck('skill_name')
            ->sort()
            ->values();
    }

    public function render()
    {
        return view('livewire.technician-management.index', [
            'technicians' => $this->technicians,
            'availableSkills' => $this->availableSkills
        ]);
    }
}
