<?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_recordings', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->string('status')->default('recording'); // recording, processing, ready, failed
            $table->string('layout')->default('gallery');
            $table->string('storage_path')->nullable();
            $table->string('thumbnail_path')->nullable();
            $table->unsignedBigInteger('file_size')->nullable();
            $table->unsignedInteger('duration_seconds')->nullable();
            $table->string('started_by_ref')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('ended_at')->nullable();
            $table->text('failure_reason')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();
            $table->index(['owner_ref', 'status']);
        });

        Schema::create('meet_transcripts', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->foreignId('recording_id')->nullable()->constrained('meet_recordings')->nullOnDelete();
            $table->string('status')->default('pending'); // pending, processing, ready, failed
            $table->longText('content')->nullable(); // plain searchable text
            $table->json('segments')->nullable(); // [{speaker, text, start, end}]
            $table->boolean('live_captions_enabled')->default(false);
            $table->timestamps();
        });

        Schema::create('meet_ai_summaries', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->foreignId('transcript_id')->nullable()->constrained('meet_transcripts')->nullOnDelete();
            $table->string('status')->default('pending'); // pending, processing, ready, failed
            $table->text('summary')->nullable();
            $table->json('decisions')->nullable();
            $table->timestamps();
        });

        Schema::create('meet_action_items', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('ai_summary_id')->constrained('meet_ai_summaries')->cascadeOnDelete();
            $table->string('assignee_ref')->nullable();
            $table->string('assignee_name')->nullable();
            $table->text('description');
            $table->date('due_date')->nullable();
            $table->string('status')->default('open'); // open, done, cancelled
            $table->timestamps();
        });

        Schema::create('meet_templates', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('meet_organizations')->cascadeOnDelete();
            $table->string('name');
            $table->text('description')->nullable();
            $table->unsignedInteger('duration_minutes')->default(60);
            $table->json('settings')->nullable();
            $table->boolean('is_default')->default(false);
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::table('meet_rooms', function (Blueprint $table) {
            $table->foreignId('template_id')->nullable()->after('source')->constrained('meet_templates')->nullOnDelete();
            $table->uuid('recurring_series_id')->nullable()->after('template_id')->index();
            $table->string('recurrence_rule')->nullable()->after('recurring_series_id'); // daily, weekly, monthly
            $table->unsignedTinyInteger('recurrence_interval')->default(1)->after('recurrence_rule');
            $table->date('recurrence_until')->nullable()->after('recurrence_interval');
        });

        Schema::table('meet_sessions', function (Blueprint $table) {
            $table->boolean('is_locked')->default(false)->after('peak_participants');
            $table->timestamp('locked_at')->nullable()->after('is_locked');
        });

        Schema::table('meet_organizations', function (Blueprint $table) {
            $table->json('security_policy')->nullable()->after('settings');
        });
    }

    public function down(): void
    {
        Schema::table('meet_organizations', function (Blueprint $table) {
            $table->dropColumn('security_policy');
        });

        Schema::table('meet_sessions', function (Blueprint $table) {
            $table->dropColumn(['is_locked', 'locked_at']);
        });

        Schema::table('meet_rooms', function (Blueprint $table) {
            $table->dropConstrainedForeignId('template_id');
            $table->dropColumn(['recurring_series_id', 'recurrence_rule', 'recurrence_interval', 'recurrence_until']);
        });

        Schema::dropIfExists('meet_action_items');
        Schema::dropIfExists('meet_ai_summaries');
        Schema::dropIfExists('meet_transcripts');
        Schema::dropIfExists('meet_recordings');
        Schema::dropIfExists('meet_templates');
    }
};
