<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\LogsModelActivity;

/**
 * @OA\Schema(
 *     schema="Room",
 *     type="object",
 *     @OA\Property(property="id", type="integer", example=16),
 *     @OA\Property(property="floor", type="integer", example="1"),
 *     @OA\Property(property="type", ref="#/components/schemas/RoomType"),
 *     @OA\Property(property="name", type="string", example="pc102"),
 *     @OA\Property(property="internal_ip", type="string", example="192.168.11.7"),
 *     @OA\Property(property="port", type="int", example="9000"),
 *     @OA\Property(property="status", ref="#/components/schemas/RoomStatus"),
 *     @OA\Property(property="started_at", type="string", format="date-time", example="2025-05-11T16:00:00.000000Z"),
 *     @OA\Property(property="ended_at", type="string", format="date-time", example=null),
 * )
 */

class Room extends Model
{
    /** @use HasFactory<\Database\Factories\ArtistFactory> */
    use HasFactory, LogsModelActivity;

    public string $log_message = 'BranchForm-add';
    public string $log_source = 'manual';

    protected $fillable = [
        'branch_id',
        'floor',
        'type',
        'name', 
        'internal_ip',
        'port',
        'is_online',
        'status',
        'started_at',
        'ended_at',
    ];
    protected $attributes = [
        'type' => \App\Enums\RoomType::Unset,
        'floor' => 1,
        'is_online' => false,
        'status' => \App\Enums\RoomStatus::Error,
    ];

    protected $hidden = [
        'internal_ip',
        'port',
    ];

    protected $casts = [
        'floor' => 'int',
        'type' => \App\Enums\RoomType::class,
        'name' => 'string',
        'internal_ip' =>'string',
        'port' => 'int',
        'is_online' => 'boolean',
        'status' => \App\Enums\RoomStatus::class,
        'started_at' => 'datetime',
        'ended_at' => 'datetime',
    ];

    public function str_started_at(){
        $str ="Not Set";
        if($this->started_at !=null){
            $str = $this->started_at;
        }
        return $str;
    }

    public function str_ended_at(){
        $str ="Not Set";
        if($this->ended_at !=null){
            $str = $this->ended_at;
        }
        return $str;
    }

    public function branch() {
        return $this->belongsTo(Branch::class);
    }
    public function full_name(){
        return $this->type->labelPowergridFilter().$this->name;
    }

    public function statusLogs() : HasMany
    {
        return $this->hasMany(RoomStatusLog::class);
    }
    public function sessions() : HasMany
    {
        return $this->hasMany(RoomSession::class);
    }
    /**
     * 最新的一筆 session
     */
    public function latestSession() : HasOne
    {
        return $this->hasOne(RoomSession::class)->latestOfMany();
    }

    /**
     * 方便取得最新狀態
     */
    public function getLatestStatusAttribute() : string
    {
        return $this->latestSession?->status ?? 'error';
    }
}
