<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('meet_rooms', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('meet_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('meet_branches')->nullOnDelete();
            $table->string('host_user_ref')->index();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('type')->default('instant'); // instant, scheduled, personal, recurring
            $table->string('status')->default('scheduled'); // scheduled, live, ended, cancelled
            $table->string('slug')->nullable()->index();
            $table->timestamp('scheduled_at')->nullable()->index();
            $table->unsignedInteger('duration_minutes')->nullable();
            $table->string('timezone')->default('UTC');
            $table->string('passcode')->nullable();
            $table->json('settings')->nullable();
            $table->json('source')->nullable(); // {app, entity_type, entity_id}
            $table->timestamps();
            $table->softDeletes();
            $table->index(['owner_ref', 'status', 'scheduled_at']);
        });

        Schema::create('meet_sessions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('room_id')->constrained('meet_rooms')->cascadeOnDelete();
            $table->string('media_room_name')->index();
            $table->string('status')->default('live'); // live, ended
            $table->timestamp('started_at')->nullable();
            $table->timestamp('ended_at')->nullable();
            $table->unsignedInteger('peak_participants')->default(0);
            $table->timestamps();
        });

        Schema::create('meet_participants', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->string('user_ref')->nullable()->index();
            $table->string('display_name');
            $table->string('email')->nullable();
            $table->string('role')->default('participant'); // host, co_host, participant, guest
            $table->string('status')->default('invited'); // invited, waiting, joined, left, removed
            $table->timestamp('joined_at')->nullable();
            $table->timestamp('left_at')->nullable();
            $table->string('device_info')->nullable();
            $table->string('ip_address')->nullable();
            $table->boolean('hand_raised')->default(false);
            $table->boolean('is_muted')->default(false);
            $table->boolean('is_video_off')->default(false);
            $table->timestamps();
            $table->index(['session_id', 'status']);
        });

        Schema::create('meet_invitations', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('room_id')->constrained('meet_rooms')->cascadeOnDelete();
            $table->string('email')->nullable();
            $table->string('user_ref')->nullable();
            $table->string('display_name')->nullable();
            $table->string('role')->default('participant');
            $table->string('status')->default('pending'); // pending, accepted, declined
            $table->timestamp('sent_at')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('meet_invitations');
        Schema::dropIfExists('meet_participants');
        Schema::dropIfExists('meet_sessions');
        Schema::dropIfExists('meet_rooms');
    }
};
