<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class TestingPanelReading extends Model
{
    use Prunable;
    use SoftDeletes;

    protected $fillable = [
        'plant_id',
        'line_id',
        'motor_testing_master_id',
        'machine_id',
        'output',
        'serial_number',
        'winded_serial_number',
        'before_fr_volt',
        'before_fr_cur',
        'before_fr_pow',
        'before_fr_res_ry',
        'before_fr_res_yb',
        'before_fr_res_br',
        'before_fr_ir',
        'before_fr_ir_r',
        'before_fr_ir_y',
        'before_fr_ir_b',
        'before_fr_freq',
        'before_fr_speed',
        'after_fr_vol',
        'after_fr_cur',
        'after_fr_pow',
        'after_fr_ir_hot',
        'after_fr_ir_hot_r',
        'after_fr_ir_hot_y',
        'after_fr_ir_hot_b',
        'after_fr_ir_cool',
        'after_fr_ir_cool_r',
        'after_fr_ir_cool_y',
        'after_fr_ir_cool_b',
        'after_fr_freq',
        'after_fr_speed',
        'after_fr_leak_cur',
        'locked_rt_volt',
        'locked_rt_cur',
        'locked_rt_pow',
        'no_load_pickup_volt',
        'room_temperature',
        'hv_test',
        'batch_number',
        'batch_count',
        'result',
        'remark',
        'rework_count',
        'update_count',
        'output_flag',
        'tested_by',
        'updated_by',
        'created_at',
        'updated_at',
        'scanned_at',
    ];

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

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

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

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

    public function motorTestingMaster()
    {
        return $this->belongsTo(MotorTestingMaster::class);
    }

    public function prunable(): Builder
    {
        return static::where('created_at', '<=', now()->subMonthsNoOverflow(6));
    }
}
