<?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_channels', 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('name');
            $table->string('slug');
            $table->string('visibility')->default('public'); // public, private
            $table->text('description')->nullable();
            $table->timestamps();
            $table->unique(['organization_id', 'slug']);
        });

        Schema::create('meet_channel_members', function (Blueprint $table) {
            $table->id();
            $table->foreignId('channel_id')->constrained('meet_channels')->cascadeOnDelete();
            $table->string('user_ref')->index();
            $table->string('role')->default('member'); // admin, moderator, member
            $table->timestamps();
            $table->unique(['channel_id', 'user_ref']);
        });

        Schema::create('meet_channel_messages', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('channel_id')->constrained('meet_channels')->cascadeOnDelete();
            $table->string('sender_ref')->nullable();
            $table->string('sender_name');
            $table->text('body');
            $table->foreignId('parent_id')->nullable()->constrained('meet_channel_messages')->nullOnDelete();
            $table->json('metadata')->nullable();
            $table->timestamps();
            $table->index(['channel_id', 'created_at']);
        });

        Schema::create('meet_direct_conversations', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('meet_organizations')->cascadeOnDelete();
            $table->string('type')->default('dm'); // dm, group
            $table->string('title')->nullable();
            $table->timestamps();
        });

        Schema::create('meet_direct_participants', function (Blueprint $table) {
            $table->id();
            $table->foreignId('conversation_id')->constrained('meet_direct_conversations')->cascadeOnDelete();
            $table->string('user_ref')->index();
            $table->timestamps();
            $table->unique(['conversation_id', 'user_ref']);
        });

        Schema::create('meet_direct_messages', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('conversation_id')->constrained('meet_direct_conversations')->cascadeOnDelete();
            $table->string('sender_ref')->nullable();
            $table->string('sender_name');
            $table->text('body');
            $table->foreignId('parent_id')->nullable()->constrained('meet_direct_messages')->nullOnDelete();
            $table->timestamps();
            $table->index(['conversation_id', 'created_at']);
        });

        Schema::create('meet_live_streams', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('session_id')->constrained('meet_sessions')->cascadeOnDelete();
            $table->string('platform'); // youtube, facebook, linkedin, custom
            $table->string('rtmp_url');
            $table->text('stream_key');
            $table->string('status')->default('idle'); // idle, live, stopped, failed
            $table->string('layout')->default('speaker');
            $table->string('health')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('ended_at')->nullable();
            $table->timestamps();
        });

        Schema::create('meet_usage_records', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('meet_organizations')->cascadeOnDelete();
            $table->foreignId('branch_id')->nullable()->constrained('meet_branches')->nullOnDelete();
            $table->string('metric'); // participant_minutes, recording_storage_bytes, ai_minutes
            $table->unsignedBigInteger('quantity')->default(0);
            $table->json('metadata')->nullable();
            $table->timestamp('recorded_at');
            $table->timestamps();
            $table->index(['organization_id', 'metric', 'recorded_at']);
        });

        Schema::table('meet_organizations', function (Blueprint $table) {
            $table->string('license_tier')->default('standard')->after('timezone');
            $table->json('usage_quotas')->nullable()->after('license_tier');
        });

        Schema::table('meet_sessions', function (Blueprint $table) {
            $table->string('session_mode')->default('live')->after('status'); // live, green_room, practice
            $table->json('presenter_refs')->nullable()->after('session_mode');
        });
    }

    public function down(): void
    {
        Schema::table('meet_sessions', function (Blueprint $table) {
            $table->dropColumn(['session_mode', 'presenter_refs']);
        });

        Schema::table('meet_organizations', function (Blueprint $table) {
            $table->dropColumn(['license_tier', 'usage_quotas']);
        });

        Schema::dropIfExists('meet_usage_records');
        Schema::dropIfExists('meet_live_streams');
        Schema::dropIfExists('meet_direct_messages');
        Schema::dropIfExists('meet_direct_participants');
        Schema::dropIfExists('meet_direct_conversations');
        Schema::dropIfExists('meet_channel_messages');
        Schema::dropIfExists('meet_channel_members');
        Schema::dropIfExists('meet_channels');
    }
};
