<?php

namespace App\Models\Equipment;

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

class EquipmentInspection extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'tenant_id',
        'equipment_id',
        'year_month',
        'overall_judgment',
        'inspector_id',
        'repair_note',
        'issue_note',
        'created_by',
        'updated_by',
    ];

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

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

    public function details(): HasMany
    {
        return $this->hasMany(EquipmentInspectionDetail::class, 'inspection_id');
    }

    public function getJudgmentLabelAttribute(): string
    {
        return match ($this->overall_judgment) {
            'OK' => '양호',
            'NG' => '이상',
            default => '-',
        };
    }

    public function getJudgmentColorAttribute(): string
    {
        return match ($this->overall_judgment) {
            'OK' => 'bg-green-100 text-green-800',
            'NG' => 'bg-red-100 text-red-800',
            default => 'bg-gray-100 text-gray-800',
        };
    }
}
