<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Device extends Model
{
    use HasFactory;

    protected $table = 'devices';

    // Ensure UUIDs are treated as strings
    protected $keyType = 'string';        // Treat ID as a string
    public $incrementing = false;         // Disable auto-incrementing

    protected $fillable = [
        'id',
        'name',
        'asset_id',
        'type',
        'device_profile_id',
        'tenant_id',
        'customer_id',
        'speed_limit',
        'torque_limit',
        'power_limit',
        'label',
        'version',
        'active',
        'additional_info',
        'device_data',
        'created_time',
    ];

    // Cast UUIDs and related IDs as strings
    protected $casts = [
        'id' => 'string',
        'asset_id' => 'string',
        'device_profile_id' => 'string'
    ];

    // Relationships
    public function asset()
    {
        return $this->belongsTo(Asset::class, 'asset_id', 'id');
    }

    public function deviceProfile()
    {
        return $this->belongsTo(DeviceProfileMaster::class, 'device_profile_id', 'id');
    }

    public function timeseriesKeys()
    {
        return $this->hasMany(TimeseriesKeyMaster::class, 'device_profile_xid', 'device_profile_id');
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class, 'customer_id', 'id');
    }
}
