<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class Terminal extends Model
{
    use HasFactory, SoftDeletes, LogsActivity;

    /**
     * 可批量赋值的属性
     *
     * @var array<string>
     */
    protected $fillable = [
        'name',
        'code',
        'ip_address',
        'mac_address',
        'station_id',
        'diagram_urls',
        'scada_data_url',
        'scada_tags_url',
        'prompt_template',
        'voice_wakeup_enabled',
        'voice_wakeup_word',
        'is_online',
        'last_online_at',
    ];

    /**
     * 属性类型转换
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'diagram_urls' => 'array',
            'voice_wakeup_enabled' => 'boolean',
            'is_online' => 'boolean',
            'last_online_at' => 'datetime',
        ];
    }

    /**
     * 获取终端所属的线站
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function station()
    {
        return $this->belongsTo(Station::class);
    }

    /**
     * 配置活动日志选项
     *
     * @return \Spatie\Activitylog\LogOptions
     */
    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['name', 'code', 'station_id', 'diagram_urls'])
            ->logOnlyDirty()
            ->setDescriptionForEvent(fn(string $eventName) => "终端已{$eventName}");
    }
}
