<?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_calendar_connections', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('meet_organizations')->cascadeOnDelete();
            $table->string('user_ref')->index();
            $table->string('provider'); // google, microsoft, apple
            $table->text('access_token')->nullable();
            $table->text('refresh_token')->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->string('calendar_id')->nullable();
            $table->json('metadata')->nullable();
            $table->timestamps();
            $table->unique(['user_ref', 'provider']);
        });

        Schema::create('meet_contact_groups', 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->timestamps();
        });

        Schema::create('meet_contact_group_members', function (Blueprint $table) {
            $table->id();
            $table->foreignId('contact_group_id')->constrained('meet_contact_groups')->cascadeOnDelete();
            $table->string('email')->nullable();
            $table->string('user_ref')->nullable();
            $table->string('display_name')->nullable();
            $table->timestamps();
            $table->index(['contact_group_id', 'email']);
        });

        Schema::create('meet_webhook_endpoints', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('meet_organizations')->cascadeOnDelete();
            $table->string('url');
            $table->string('secret')->nullable();
            $table->json('events')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::table('meet_invitations', function (Blueprint $table) {
            $table->string('rsvp_token')->nullable()->unique()->after('status');
            $table->timestamp('responded_at')->nullable()->after('rsvp_token');
            $table->string('calendar_event_id')->nullable()->after('responded_at');
        });

        Schema::table('meet_rooms', function (Blueprint $table) {
            $table->string('calendar_event_id')->nullable()->after('source');
        });
    }

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

        Schema::table('meet_invitations', function (Blueprint $table) {
            $table->dropColumn(['rsvp_token', 'responded_at', 'calendar_event_id']);
        });

        Schema::dropIfExists('meet_webhook_endpoints');
        Schema::dropIfExists('meet_contact_group_members');
        Schema::dropIfExists('meet_contact_groups');
        Schema::dropIfExists('meet_calendar_connections');
    }
};
