<?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\SensorStatus;

/**
 * 传感器模型
 *
 * 管理农业物联网传感器设备信息。
 *
 * @property string $device_id   设备编号
 * @property string $name        传感器名称
 * @property string $type        传感器类型
 * @property string $status      在线状态
 */
class Sensor extends Model
{
    protected $table = 'kylin_agri_sensors';

    protected $fillable = [
        'device_id',
        'type',
        'plot_id',
        'parameters',
        'frequency',
        'status',
        'last_data_at',
    ];

    protected function casts(): array
    {
        return [
            'parameters' => 'array',
            'frequency' => 'integer',
            'status' => SensorStatus::class,
            'last_data_at' => 'datetime',
        ];
    }

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

    public function sensorData(): HasMany
    {
        return $this->hasMany(SensorData::class);
    }
}
