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

    protected $fillable = [
        'plant_id',
        'mfm_meter_id',
        'apparent_energy_received',
        'reactive_energy_received',
        'active_energy_received',
        'active_power_r',
        'active_power_y',
        'active_power_b',
        'active_power_total',
        'voltage_ry',
        'voltage_yb',
        'voltage_br',
        'current_r',
        'current_y',
        'current_b',
        'current_n',
        'voltage_r_n',
        'voltage_y_n',
        'voltage_b_n',
        'frequency',
        'power_factor_r',
        'power_factor_y',
        'power_factor_b',
        'power_factor_total',
        'created_at',
        'updated_at',
        'created_by',
        'updated_by',
    ];

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

    public function mfmMeter(): BelongsTo
    {
        return $this->belongsTo(MfmMeter::class, 'mfm_meter_id');
    }

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