<?php

namespace Kylin\Logistics\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kylin\Logistics\Enums\AlarmType;

/**
 * 报警模型
 *
 * 记录车辆运输过程中的异常报警，如超速、偏离路线、温度异常等。
 *
 * @property string $alarm_type      报警类型
 * @property float  $alarm_value     报警值
 * @property float  $threshold_value 阈值
 * @property bool   $is_handled      是否已处理
 */
class Alarm extends Model
{
    protected $table = 'kylin_log_alarms';

    protected $fillable = [
        'vehicle_id', 'waybill_id', 'alarm_type', 'alarm_value',
        'threshold_value', 'lng', 'lat', 'is_handled', 'handler_id',
        'handled_at', 'remark',
    ];

    protected $casts = [
        'alarm_type' => AlarmType::class,
        'alarm_value' => 'decimal:10,2',
        'threshold_value' => 'decimal:10,2',
        'lng' => 'decimal:12,8',
        'lat' => 'decimal:12,8',
        'is_handled' => 'boolean',
        'handled_at' => 'datetime',
    ];

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

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

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