<?php

namespace App\Livewire\TechnicianManagement;

use Livewire\Component;
use App\Models\Technician;
use Livewire\Attributes\On;

class TechnicianForm extends Component
{
    public $showModal = false;
    public $technicianId = null;
    public $editing = false;

    // Form fields
    public $first_name = '';
    public $last_name = '';
    public $email = '';
    public $phone = '';
    public $employee_id = '';
    public $hourly_rate = '';
    public $status = 'active';
    public $skill_level = 'junior';
    public $shift_start = '08:00';
    public $shift_end = '17:00';
    public $specializations = [];

    // Available options
    public $statusOptions = [
        'active' => 'Active',
        'inactive' => 'Inactive',
        'on_leave' => 'On Leave'
    ];

    public $skillLevelOptions = [
        'apprentice' => 'Apprentice',
        'junior' => 'Junior',
        'journeyman' => 'Journeyman',
        'intermediate' => 'Intermediate',
        'senior' => 'Senior',
        'master' => 'Master',
        'expert' => 'Expert'
    ];

    public $specializationOptions = [
        'engine' => 'Engine',
        'engine_repair' => 'Engine Repair',
        'transmission' => 'Transmission',
        'electrical_systems' => 'Electrical Systems',
        'computer_systems' => 'Computer Systems',
        'brake_systems' => 'Brake Systems',
        'brakes' => 'Brakes',
        'suspension' => 'Suspension',
        'air_conditioning' => 'Air Conditioning',
        'diagnostics' => 'Diagnostics',
        'diagnostic' => 'Diagnostic',
        'bodywork' => 'Bodywork',
        'painting' => 'Painting',
        'paint' => 'Paint',
        'general_maintenance' => 'General Maintenance'
    ];

    protected $rules = [
        'first_name' => 'required|string|max:255',
        'last_name' => 'required|string|max:255',
        'email' => 'required|email|max:255',
        'phone' => 'required|string|max:20',
        'employee_id' => 'required|string|max:50',
        'hourly_rate' => 'required|numeric|min:0',
        'status' => 'required|in:active,inactive,on_leave',
        'skill_level' => 'required|in:apprentice,junior,journeyman,intermediate,senior,master,expert',
        'shift_start' => 'required|date_format:H:i',
        'shift_end' => 'required|date_format:H:i|after:shift_start',
        'specializations' => 'array'
    ];

    protected $messages = [
        'shift_end.after' => 'Shift end time must be after shift start time.',
        'employee_id.unique' => 'This employee ID is already taken.',
        'email.unique' => 'This email address is already taken.'
    ];

    #[On('create-technician')]
    public function create()
    {
        $this->resetForm();
        $this->editing = false;
        $this->showModal = true;
    }

    #[On('edit-technician')]
    public function edit($technicianId)
    {
        $technician = Technician::findOrFail($technicianId);
        
        $this->technicianId = $technician->id;
        $this->first_name = $technician->first_name;
        $this->last_name = $technician->last_name;
        $this->email = $technician->email;
        $this->phone = $technician->phone;
        $this->employee_id = $technician->employee_id;
        $this->hourly_rate = $technician->hourly_rate;
        $this->status = $technician->status;
        $this->skill_level = $technician->skill_level ?? 'junior';
        $this->shift_start = $technician->shift_start ? $technician->shift_start->format('H:i') : '08:00';
        $this->shift_end = $technician->shift_end ? $technician->shift_end->format('H:i') : '17:00';
        $this->specializations = is_array($technician->specializations) ? $technician->specializations : [];
        
        $this->editing = true;
        $this->showModal = true;
    }

    public function save()
    {
        // Add unique validation rules
        $rules = $this->rules;
        if ($this->editing) {
            $rules['employee_id'] .= ',employee_id,' . $this->technicianId;
            $rules['email'] .= ',email,' . $this->technicianId;
        } else {
            $rules['employee_id'] .= '|unique:technicians,employee_id';
            $rules['email'] .= '|unique:technicians,email';
        }

        $this->validate($rules);

        $data = [
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'email' => $this->email,
            'phone' => $this->phone,
            'employee_id' => $this->employee_id,
            'hourly_rate' => $this->hourly_rate,
            'status' => $this->status,
            'skill_level' => $this->skill_level,
            'shift_start' => $this->shift_start,
            'shift_end' => $this->shift_end,
            'specializations' => $this->specializations,
        ];

        if ($this->editing) {
            $technician = Technician::findOrFail($this->technicianId);
            $technician->update($data);
            session()->flash('message', 'Technician updated successfully!');
        } else {
            Technician::create($data);
            session()->flash('message', 'Technician created successfully!');
        }

        $this->resetForm();
        $this->showModal = false;
        $this->dispatch('technician-saved');
    }

    public function closeModal()
    {
        $this->resetForm();
        $this->showModal = false;
    }

    public function resetForm()
    {
        $this->technicianId = null;
        $this->first_name = '';
        $this->last_name = '';
        $this->email = '';
        $this->phone = '';
        $this->employee_id = '';
        $this->hourly_rate = '';
        $this->status = 'active';
        $this->skill_level = 'junior';
        $this->shift_start = '08:00';
        $this->shift_end = '17:00';
        $this->specializations = [];
        $this->editing = false;
        $this->resetErrorBag();
    }

    public function render()
    {
        return view('livewire.technician-management.technician-form');
    }
}
