<?php

namespace App\Livewire\TechnicianManagement;

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

class SkillsManagement extends Component
{
    public $showModal = false;
    public $technicianId = null;
    public $technician = null;
    public $skillId = null;
    public $editing = false;

    // Form fields
    public $skill_name = '';
    public $category = '';
    public $proficiency_level = 1;
    public $certification_body = '';
    public $certification_expires = '';
    public $is_primary_skill = false;
    public $notes = '';

    protected $rules = [
        'skill_name' => 'required|string|max:255',
        'category' => 'required|string|max:255',
        'proficiency_level' => 'required|integer|min:1|max:5',
        'certification_body' => 'nullable|string|max:255',
        'certification_expires' => 'nullable|date',
        'is_primary_skill' => 'boolean',
        'notes' => 'nullable|string|max:1000'
    ];

    #[On('manage-skills')]
    public function manageSkills($technicianId)
    {
        $this->technicianId = $technicianId;
        $this->technician = Technician::with('skills')->findOrFail($technicianId);
        $this->showModal = true;
        $this->resetForm();
    }

    public function addSkill()
    {
        $this->resetForm();
        $this->editing = false;
    }

    public function editSkill($skillId)
    {
        $skill = TechnicianSkill::findOrFail($skillId);
        
        $this->skillId = $skill->id;
        $this->skill_name = $skill->skill_name;
        $this->category = $skill->category;
        $this->proficiency_level = $skill->proficiency_level;
        $this->certification_body = $skill->certification_body;
        $this->certification_expires = $skill->certification_expires ? $skill->certification_expires->format('Y-m-d') : '';
        $this->is_primary_skill = $skill->is_primary_skill;
        $this->notes = $skill->notes;
        
        $this->editing = true;
    }

    public function saveSkill()
    {
        $this->validate();

        $data = [
            'technician_id' => $this->technicianId,
            'skill_name' => $this->skill_name,
            'category' => $this->category,
            'proficiency_level' => $this->proficiency_level,
            'certification_body' => $this->certification_body,
            'certification_expires' => $this->certification_expires ?: null,
            'is_primary_skill' => $this->is_primary_skill,
            'notes' => $this->notes,
        ];

        if ($this->editing) {
            $skill = TechnicianSkill::findOrFail($this->skillId);
            $skill->update($data);
            session()->flash('message', 'Skill updated successfully!');
        } else {
            TechnicianSkill::create($data);
            session()->flash('message', 'Skill added successfully!');
        }

        $this->technician->refresh();
        $this->resetForm();
    }

    public function deleteSkill($skillId)
    {
        TechnicianSkill::findOrFail($skillId)->delete();
        $this->technician->refresh();
        session()->flash('message', 'Skill removed successfully!');
    }

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

    public function resetForm()
    {
        $this->skillId = null;
        $this->skill_name = '';
        $this->category = '';
        $this->proficiency_level = 1;
        $this->certification_body = '';
        $this->certification_expires = '';
        $this->is_primary_skill = false;
        $this->notes = '';
        $this->editing = false;
        $this->resetErrorBag();
    }

    public function getSkillCategoriesProperty()
    {
        return TechnicianSkill::getSkillCategories();
    }

    public function getCommonSkillsProperty()
    {
        return TechnicianSkill::getCommonSkills();
    }

    public function getProficiencyLevelsProperty()
    {
        return TechnicianSkill::getProficiencyLevels();
    }

    public function render()
    {
        return view('livewire.technician-management.skills-management', [
            'skillCategories' => $this->skillCategories,
            'commonSkills' => $this->commonSkills,
            'proficiencyLevels' => $this->proficiencyLevels
        ]);
    }
}
