<?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 RequestCharacteristic extends Model
{
    use Prunable;
    use SoftDeletes;

    protected $fillable = [
        'plant_id',
        'machine_id',
        'item_id',
        'characteristic_approver_master_id',
        'aufnr',
        'characteristic_name',
        'current_value',
        'update_value',
        'approver_status1',
        'approver_status2',
        'approver_status3',
        'approver_remark1',
        'approver_remark2',
        'approver_remark3',
        'approved1_at',
        'approved2_at',
        'approved3_at',
        'mail_status',
        'trigger_at',
        'work_flow_id',
        'model_type',
        'created_at',
        'updated_at',
        'created_by',
        'updated_by',
    ];

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

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

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

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

    // public function approverName1()
    // {
    //     return $this->belongsTo(
    //         CharacteristicApproverMaster::class,
    //         'characteristic_approver_master_id'
    //     );
    // }

    // public function approverName2()
    // {
    //     return $this->belongsTo(
    //         CharacteristicApproverMaster::class,
    //         'characteristic_approver_master_id'
    //     );
    // }

    // public function approverName3()
    // {
    //     return $this->belongsTo(
    //         CharacteristicApproverMaster::class,
    //         'characteristic_approver_master_id'
    //     );
    // }

    public function approver()
    {
        return $this->belongsTo(
            CharacteristicApproverMaster::class,
            'characteristic_approver_master_id'
        );
    }

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