<?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
    {
        if (! Schema::hasTable('mexco_organisations')) {
            Schema::create('mexco_organisations', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->string('slug')->unique();
                $table->string('type', 64)->default('other');
                $table->string('contact_person')->nullable();
                $table->string('email')->nullable();
                $table->string('phone')->nullable();
                $table->string('status', 32)->default('active');
                $table->json('metadata')->nullable();
                $table->timestamps();
                $table->softDeletes();

                $table->index('status');
            });
        }

        if (! Schema::hasTable('mexco_organisation_users')) {
            Schema::create('mexco_organisation_users', function (Blueprint $table) {
                $table->id();
                $table->foreignId('organisation_id')->constrained('mexco_organisations')->cascadeOnDelete();
                $table->string('name');
                $table->string('email');
                $table->string('password');
                $table->string('role', 32)->default('event_manager');
                $table->string('status', 32)->default('active');
                $table->timestamp('invited_at')->nullable();
                $table->timestamp('last_login_at')->nullable();
                $table->timestamps();

                $table->unique(['organisation_id', 'email'], 'mexco_org_users_org_email_unique');
                $table->index('email');
            });
        }

        if (! Schema::hasTable('mexco_organisation_user_events')) {
            Schema::create('mexco_organisation_user_events', function (Blueprint $table) {
                $table->id();
                $table->foreignId('organisation_user_id')->constrained('mexco_organisation_users')->cascadeOnDelete();
                $table->string('event_id');
                $table->string('role', 32)->nullable();
                $table->timestamps();

                $table->foreign('event_id')->references('id')->on('meetup_events')->cascadeOnDelete();
                $table->unique(['organisation_user_id', 'event_id'], 'mexco_org_user_evt_unique');
            });
        }

        Schema::table('meetup_events', function (Blueprint $table) {
            if (! Schema::hasColumn('meetup_events', 'organisation_id')) {
                $table->foreignId('organisation_id')->nullable()->after('id')->constrained('mexco_organisations')->nullOnDelete();
            }
            if (! Schema::hasColumn('meetup_events', 'platform_status')) {
                $table->string('platform_status', 32)->default('active')->after('ends_at');
            }
            if (! Schema::hasColumn('meetup_events', 'archived_at')) {
                $table->timestamp('archived_at')->nullable()->after('platform_status');
            }
        });
    }

    public function down(): void
    {
        Schema::table('meetup_events', function (Blueprint $table) {
            if (Schema::hasColumn('meetup_events', 'organisation_id')) {
                $table->dropForeign(['organisation_id']);
                $table->dropColumn(['organisation_id', 'platform_status', 'archived_at']);
            }
        });

        Schema::dropIfExists('mexco_organisation_user_events');
        Schema::dropIfExists('mexco_organisation_users');
        Schema::dropIfExists('mexco_organisations');
    }
};
