<?php

namespace App\Models;

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

class AlertSetting extends Model
{
    protected $fillable = [
        'olt_device_id', 'warning_threshold', 'critical_threshold',
        'email_enabled', 'email_recipients', 'active',
    ];

    protected $casts = [
        'warning_threshold'  => 'decimal:3',
        'critical_threshold' => 'decimal:3',
        'email_enabled'      => 'boolean',
        'active'             => 'boolean',
    ];

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

    public static function getForDevice(?int $deviceId = null): self
    {
        return static::where('olt_device_id', $deviceId)->first()
            ?? new static([
                'olt_device_id'      => $deviceId,
                'warning_threshold'  => -25.000,
                'critical_threshold' => -28.000,
                'email_enabled'      => false,
                'active'             => true,
            ]);
    }
}
