<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class Session extends Model
{
    use BelongsToOwner;

    protected $table = 'meet_sessions';

    protected $fillable = [
        'uuid', 'owner_ref', 'room_id', 'parent_session_id', 'media_room_name', 'status',
        'session_mode', 'presenter_refs', 'is_breakout', 'started_at', 'ended_at',
        'peak_participants', 'is_locked', 'locked_at',
    ];

    protected function casts(): array
    {
        return [
            'started_at' => 'datetime',
            'ended_at' => 'datetime',
            'locked_at' => 'datetime',
            'is_locked' => 'boolean',
            'is_breakout' => 'boolean',
            'presenter_refs' => 'array',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (Session $session) {
            if (empty($session->uuid)) {
                $session->uuid = (string) Str::uuid();
            }
        });
    }

    public function getRouteKeyName(): string
    {
        return 'uuid';
    }

    public function room(): BelongsTo
    {
        return $this->belongsTo(Room::class, 'room_id');
    }

    public function participants(): HasMany
    {
        return $this->hasMany(Participant::class, 'session_id');
    }

    public function messages(): HasMany
    {
        return $this->hasMany(Message::class, 'session_id');
    }

    public function reactions(): HasMany
    {
        return $this->hasMany(Reaction::class, 'session_id');
    }

    public function recordings(): HasMany
    {
        return $this->hasMany(Recording::class, 'session_id');
    }

    public function transcripts(): HasMany
    {
        return $this->hasMany(Transcript::class, 'session_id');
    }

    public function aiSummaries(): HasMany
    {
        return $this->hasMany(AiSummary::class, 'session_id');
    }

    public function qaQuestions(): HasMany
    {
        return $this->hasMany(QaQuestion::class, 'session_id');
    }

    public function polls(): HasMany
    {
        return $this->hasMany(MeetPoll::class, 'session_id');
    }

    public function breakoutRooms(): HasMany
    {
        return $this->hasMany(BreakoutRoom::class, 'session_id');
    }

    public function whiteboard(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        return $this->hasOne(Whiteboard::class, 'session_id');
    }

    public function sessionFiles(): HasMany
    {
        return $this->hasMany(SessionFile::class, 'session_id');
    }

    public function liveStreams(): HasMany
    {
        return $this->hasMany(LiveStream::class, 'session_id');
    }

    public function activeParticipants(): HasMany
    {
        return $this->participants()->where('status', 'joined');
    }

    public function isLive(): bool
    {
        return $this->status === 'live';
    }
}
