<?php

namespace App\Models\Tenant;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Labor extends Model
{
    use BelongsToTenant, SoftDeletes;

    public const CATEGORIES = [
        'Motor', 'Frâne', 'Suspensie', 'Anvelope', 'ITP', 'Cutie viteze',
        'Caroserie', 'Electrică', 'Climatizare', 'Eșapament', 'Altele',
    ];

    public const PRICING_MODES = [
        'hourly' => 'Pe oră (normă × tarif)',
        'fixed' => 'Preț fix',
    ];

    protected $fillable = [
        'company_id', 'category', 'name_ro', 'name_ru', 'code',
        'hours', 'pricing_mode', 'fixed_price', 'price', 'is_active', 'notes',
    ];

    protected $casts = [
        'hours' => 'decimal:2',
        'fixed_price' => 'decimal:2',
        'price' => 'decimal:2',
        'is_active' => 'boolean',
    ];

    public function laborParts(): HasMany
    {
        return $this->hasMany(LaborPart::class);
    }

    /** Effective line total for this labor given the tenant hourly rate. */
    public function effectiveTotal(float $hourlyRate): float
    {
        if ($this->pricing_mode === 'fixed') {
            return (float) $this->fixed_price;
        }
        return round((float) $this->hours * $hourlyRate, 2);
    }
}
