<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PowerAlert extends Model
{
    protected $fillable = [
        'olt_device_id', 'frame', 'slot', 'port', 'onu_id',
        'serial_number', 'olt_rx_power', 'onu_rx_power',
        'severity', 'acknowledged', 'notified',
    ];

    protected $casts = [
        'olt_rx_power' => 'decimal:3',
        'onu_rx_power' => 'decimal:3',
        'acknowledged' => 'boolean',
        'notified'     => 'boolean',
    ];

    public function device(): BelongsTo
    {
        return $this->belongsTo(OltDevice::class, 'olt_device_id');
    }

    public function scopeUnacknowledged($query)
    {
        return $query->where('acknowledged', false);
    }

    public function getLocationAttribute(): string
    {
        return "gpon-olt_{$this->frame}/{$this->slot}/{$this->port}:{$this->onu_id}";
    }
}
