<?php

namespace App\Models\Equipment;

use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class EquipmentRepair extends Model
{
    use BelongsToTenant, SoftDeletes;

    protected $fillable = [
        'tenant_id',
        'equipment_id',
        'repair_date',
        'repair_type',
        'repair_hours',
        'description',
        'cost',
        'vendor',
        'repaired_by',
        'memo',
        'created_by',
        'updated_by',
    ];

    protected $casts = [
        'repair_date' => 'date',
        'repair_hours' => 'decimal:1',
        'cost' => 'decimal:2',
    ];

    public function equipment(): BelongsTo
    {
        return $this->belongsTo(Equipment::class, 'equipment_id');
    }

    public function repairer(): BelongsTo
    {
        return $this->belongsTo(\App\Models\User::class, 'repaired_by');
    }

    public function getRepairTypeLabelAttribute(): string
    {
        return match ($this->repair_type) {
            'internal' => '사내',
            'external' => '외주',
            default => $this->repair_type ?? '-',
        };
    }

    public function getFormattedCostAttribute(): string
    {
        if (! $this->cost) {
            return '-';
        }

        return number_format($this->cost).'원';
    }
}
