<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class TechnicianSkill extends Model
{
    use HasFactory;

    protected $fillable = [
        'technician_id',
        'skill_name',
        'category',
        'proficiency_level',
        'certified_date',
        'certification_expires',
        'certification_body',
        'notes',
        'is_primary_skill',
    ];

    protected $casts = [
        'certified_date' => 'date',
        'certification_expires' => 'date',
        'is_primary_skill' => 'boolean',
    ];

    public function technician(): BelongsTo
    {
        return $this->belongsTo(Technician::class);
    }

    public function getProficiencyLabelAttribute(): string
    {
        return match($this->proficiency_level) {
            1 => 'Beginner',
            2 => 'Basic',
            3 => 'Intermediate',
            4 => 'Advanced',
            5 => 'Expert',
            default => 'Unknown',
        };
    }

    public function isCertificationExpiring(int $days = 30): bool
    {
        if (!$this->certification_expires) {
            return false;
        }

        return $this->certification_expires->diffInDays(now()) <= $days;
    }

    public function isCertificationExpired(): bool
    {
        if (!$this->certification_expires) {
            return false;
        }

        return $this->certification_expires->isPast();
    }

    public static function getSkillCategories(): array
    {
        return [
            'engine' => 'Engine & Powertrain',
            'electrical' => 'Electrical Systems',
            'transmission' => 'Transmission',
            'brakes' => 'Brakes & Suspension',
            'hvac' => 'HVAC & Climate',
            'diagnostics' => 'Diagnostics',
            'bodywork' => 'Body & Paint',
            'electronics' => 'Electronics & Infotainment',
            'hybrid' => 'Hybrid & Electric',
            'general' => 'General Maintenance',
        ];
    }

    public static function getProficiencyLevels(): array
    {
        return [
            1 => 'Beginner',
            2 => 'Basic',
            3 => 'Intermediate',
            4 => 'Advanced',
            5 => 'Expert',
        ];
    }

    public static function getCommonSkills(): array
    {
        return [
            'engine' => [
                'Engine Diagnostics',
                'Engine Rebuild',
                'Timing Belt Replacement',
                'Oil Change',
                'Cooling System Repair',
            ],
            'electrical' => [
                'Wiring Repair',
                'Battery Replacement',
                'Alternator Repair',
                'Starter Motor Repair',
                'Lighting Systems',
            ],
            'transmission' => [
                'Manual Transmission Repair',
                'Automatic Transmission Service',
                'Clutch Replacement',
                'Transmission Flush',
            ],
            'brakes' => [
                'Brake Pad Replacement',
                'Brake Fluid Flush',
                'Rotor Replacement',
                'Suspension Repair',
                'Shock Absorber Replacement',
            ],
            'diagnostics' => [
                'OBD-II Diagnostics',
                'Computer Scan',
                'Emission Testing',
                'Performance Testing',
            ],
        ];
    }
}
