<?php

namespace App\Models;

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

class TechnicianWorkload extends Model
{
    use HasFactory;

    protected $fillable = [
        'technician_id',
        'workload_date',
        'scheduled_hours',
        'actual_hours',
        'billable_hours',
        'jobs_assigned',
        'jobs_completed',
        'jobs_in_progress',
        'utilization_rate',
        'efficiency_rate',
        'notes',
    ];

    protected $casts = [
        'workload_date' => 'date',
        'scheduled_hours' => 'decimal:2',
        'actual_hours' => 'decimal:2',
        'billable_hours' => 'decimal:2',
        'utilization_rate' => 'decimal:2',
        'efficiency_rate' => 'decimal:2',
    ];

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

    public function calculateUtilizationRate(): float
    {
        if ($this->scheduled_hours <= 0) {
            return 0;
        }

        return ($this->actual_hours / $this->scheduled_hours) * 100;
    }

    public function calculateEfficiencyRate(): float
    {
        if ($this->actual_hours <= 0) {
            return 0;
        }

        return ($this->billable_hours / $this->actual_hours) * 100;
    }

    public function updateCalculatedFields(): void
    {
        $this->utilization_rate = $this->calculateUtilizationRate();
        $this->efficiency_rate = $this->calculateEfficiencyRate();
    }

    public function getJobCompletionRateAttribute(): float
    {
        if ($this->jobs_assigned <= 0) {
            return 0;
        }

        return ($this->jobs_completed / $this->jobs_assigned) * 100;
    }

    public function isOverUtilized(float $threshold = 100): bool
    {
        return $this->utilization_rate > $threshold;
    }

    public function isUnderUtilized(float $threshold = 80): bool
    {
        return $this->utilization_rate < $threshold;
    }
}
