<?php

declare(strict_types=1);

namespace Kylin\Agriculture\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kylin\Agriculture\Enums\WarningLevel;
use Kylin\Agriculture\Enums\WarningStatus;

/**
 * 预警记录模型
 *
 * 记录传感器触发预警的信息，包括预警等级、状态、处理人等。
 *
 * @property string $level       预警等级
 * @property string $status      预警状态
 * @property string $message     预警信息
 * @property string $handled_at  处理时间
 */
class Warning extends Model
{
    protected $table = 'kylin_agri_warnings';

    protected $fillable = [
        'type',
        'level',
        'content',
        'plot_id',
        'sensor_id',
        'rule_id',
        'status',
        'handler_id',
        'handled_at',
    ];

    protected function casts(): array
    {
        return [
            'level' => WarningLevel::class,
            'status' => WarningStatus::class,
            'handled_at' => 'datetime',
        ];
    }

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

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

    public function rule(): BelongsTo
    {
        return $this->belongsTo(WarningRule::class, 'rule_id');
    }

    public function handler(): BelongsTo
    {
        return $this->belongsTo(config('auth.providers.users.model'), 'handler_id');
    }
}
