<?php

namespace App\Models;

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

class OltDevice extends Model
{
    protected $fillable = [
        'name', 'host', 'port', 'username', 'password', 'description',
        'default_vlan', 'default_speed_profile', 'default_onu_type', 'active',
    ];

    // Note: $hidden only affects JSON serialization, not direct property access
    // but we expose a method for clarity in the SSH service
    protected $hidden = ['password'];

    public function getPasswordRaw(): string
    {
        return \DB::table('olt_devices')->where('id', $this->id)->value('password') ?? '';
    }

    protected $casts = ['active' => 'boolean'];

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

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

    public function getPortCountAttribute(): int
    {
        return $this->ports()->count();
    }

    public function getOnuCountAttribute(): int
    {
        return OltOnu::whereIn('olt_port_id', $this->ports()->pluck('id'))->count();
    }
}
