<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Device extends Model
{
    protected $fillable = [
        'name', 'ip_address', 'port', 'width_px', 'height_px',
        'location', 'latitude', 'longitude', 'highway', 'chainage',
        'notes', 'is_active',
        'last_status', 'last_sent_mode', 'last_sent_at', 'last_seen_at',
    ];

    protected $casts = [
        'is_active'    => 'boolean',
        'latitude'     => 'float',
        'longitude'    => 'float',
        'last_sent_at' => 'datetime',
        'last_seen_at' => 'datetime',
    ];

    public function schedule(): HasOne
    {
        return $this->hasOne(DeviceSchedule::class);
    }

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

    public function getStatusBadgeAttribute(): string
    {
        return match($this->last_status) {
            'online'  => 'success',
            'offline' => 'danger',
            default   => 'secondary',
        };
    }

    public function getModeBadgeAttribute(): string
    {
        return match($this->last_sent_mode) {
            'buka'  => 'success',
            'tutup' => 'danger',
            default => 'secondary',
        };
    }
}
