<?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_webinar_registrations', 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');
            $table->string('display_name');
            $table->string('status')->default('pending'); // pending, approved, rejected
            $table->string('access_token')->nullable()->unique();
            $table->timestamp('approved_at')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();
            $table->unique(['room_id', 'email']);
        });

        Schema::create('meet_qa_questions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->string('participant_uuid')->nullable();
            $table->string('asker_name');
            $table->text('question');
            $table->string('status')->default('pending'); // pending, approved, answered, dismissed
            $table->text('answer')->nullable();
            $table->string('answered_by')->nullable();
            $table->unsignedInteger('upvotes')->default(0);
            $table->timestamps();
        });

        Schema::create('meet_polls', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->string('question');
            $table->json('options');
            $table->string('status')->default('open'); // open, closed
            $table->timestamps();
        });

        Schema::create('meet_poll_votes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('poll_id')->constrained('meet_polls')->cascadeOnDelete();
            $table->string('participant_uuid');
            $table->unsignedTinyInteger('option_index');
            $table->timestamps();
            $table->unique(['poll_id', 'participant_uuid']);
        });

        Schema::create('meet_breakout_rooms', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->string('name');
            $table->string('media_room_name');
            $table->string('status')->default('open'); // open, closed
            $table->timestamp('closes_at')->nullable();
            $table->timestamps();
        });

        Schema::create('meet_breakout_assignments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('breakout_room_id')->constrained('meet_breakout_rooms')->cascadeOnDelete();
            $table->foreignId('participant_id')->constrained('meet_participants')->cascadeOnDelete();
            $table->timestamps();
            $table->unique(['breakout_room_id', 'participant_id']);
        });

        Schema::create('meet_session_files', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('room_id')->constrained('meet_rooms')->cascadeOnDelete();
            $table->foreignId('session_id')->nullable()->constrained('meet_sessions')->nullOnDelete();
            $table->string('uploaded_by_ref')->nullable();
            $table->string('uploaded_by_name');
            $table->string('original_name');
            $table->string('storage_path');
            $table->string('mime_type')->nullable();
            $table->unsignedBigInteger('file_size')->default(0);
            $table->string('drive_file_id')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->timestamps();
        });

        Schema::create('meet_file_downloads', function (Blueprint $table) {
            $table->id();
            $table->foreignId('session_file_id')->constrained('meet_session_files')->cascadeOnDelete();
            $table->string('downloaded_by_ref')->nullable();
            $table->string('downloaded_by_name')->nullable();
            $table->string('ip_address')->nullable();
            $table->timestamp('downloaded_at');
        });

        Schema::create('meet_whiteboards', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('room_id')->constrained('meet_rooms')->cascadeOnDelete();
            $table->foreignId('session_id')->nullable()->constrained('meet_sessions')->nullOnDelete();
            $table->string('template')->default('blank');
            $table->json('state')->nullable();
            $table->timestamps();
        });

        Schema::table('meet_sessions', function (Blueprint $table) {
            $table->foreignId('parent_session_id')->nullable()->after('room_id')->constrained('meet_sessions')->nullOnDelete();
            $table->boolean('is_breakout')->default(false)->after('status');
        });

        Schema::table('meet_messages', function (Blueprint $table) {
            $table->foreignId('breakout_room_id')->nullable()->after('session_id')->constrained('meet_breakout_rooms')->nullOnDelete();
        });

        Schema::table('meet_participants', function (Blueprint $table) {
            $table->foreignId('breakout_room_id')->nullable()->after('session_id')->constrained('meet_breakout_rooms')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('meet_participants', function (Blueprint $table) {
            $table->dropConstrainedForeignId('breakout_room_id');
        });

        Schema::table('meet_messages', function (Blueprint $table) {
            $table->dropConstrainedForeignId('breakout_room_id');
        });

        Schema::table('meet_sessions', function (Blueprint $table) {
            $table->dropConstrainedForeignId('parent_session_id');
            $table->dropColumn('is_breakout');
        });

        Schema::dropIfExists('meet_whiteboards');
        Schema::dropIfExists('meet_file_downloads');
        Schema::dropIfExists('meet_session_files');
        Schema::dropIfExists('meet_breakout_assignments');
        Schema::dropIfExists('meet_breakout_rooms');
        Schema::dropIfExists('meet_poll_votes');
        Schema::dropIfExists('meet_polls');
        Schema::dropIfExists('meet_qa_questions');
        Schema::dropIfExists('meet_webinar_registrations');
    }
};
