<?php

declare(strict_types=1);

namespace Kylin\Manufacturing\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kylin\Manufacturing\Enums\EquipmentStatus;

/**
 * 设备
 */
class Equipment extends Model
{
    protected $table = 'kylin_mfg_equipment';

    protected $fillable = [
        'equipment_code',
        'equipment_name',
        'model',
        'manufacturer',
        'location',
        'production_line_id',
        'parent_id',
        'purchase_date',
        'warranty_end_date',
        'status',
        'theoretical_output_per_hour',
    ];

    protected function casts(): array
    {
        return [
            'status' => EquipmentStatus::class,
            'purchase_date' => 'date',
            'warranty_end_date' => 'date',
            'theoretical_output_per_hour' => 'integer',
        ];
    }

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

    public function parent(): BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_id');
    }

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

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

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