<?php

namespace App\Models;

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

class TechnicianPerformance extends Model
{
    use HasFactory;

    protected $fillable = [
        'technician_id',
        'performance_date',
        'metric_type',
        'metric_value',
        'period_type',
        'additional_data',
        'notes',
    ];

    protected $casts = [
        'performance_date' => 'date',
        'metric_value' => 'decimal:2',
        'additional_data' => 'array',
    ];

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

    public static function getMetricTypes(): array
    {
        return [
            'jobs_completed' => 'Jobs Completed',
            'hours_worked' => 'Hours Worked',
            'billable_hours' => 'Billable Hours',
            'revenue_generated' => 'Revenue Generated',
            'customer_rating' => 'Customer Rating',
            'efficiency_rate' => 'Efficiency Rate (%)',
            'rework_rate' => 'Rework Rate (%)',
            'safety_incidents' => 'Safety Incidents',
            'training_hours' => 'Training Hours',
            'overtime_hours' => 'Overtime Hours',
        ];
    }

    public static function getPeriodTypes(): array
    {
        return [
            'daily' => 'Daily',
            'weekly' => 'Weekly',
            'monthly' => 'Monthly',
            'quarterly' => 'Quarterly',
            'yearly' => 'Yearly',
        ];
    }

    public function getFormattedValueAttribute(): string
    {
        return match($this->metric_type) {
            'revenue_generated' => '$' . number_format($this->metric_value, 2),
            'customer_rating' => number_format($this->metric_value, 1) . '/5',
            'efficiency_rate', 'rework_rate' => number_format($this->metric_value, 1) . '%',
            'hours_worked', 'billable_hours', 'training_hours', 'overtime_hours' => number_format($this->metric_value, 1) . ' hrs',
            default => number_format($this->metric_value, 0),
        };
    }
}
