<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('tenants', function (Blueprint $table): void {
            $table->string('event_title')->nullable()->after('nombre');
            $table->string('event_location')->nullable()->after('event_title');
        });

        Schema::table('event_dates', function (Blueprint $table): void {
            $table->string('tenant_code')->nullable()->after('id');
        });

        DB::table('tenants')
            ->whereNotNull('active_event_id')
            ->orderBy('id')
            ->each(function (object $tenant): void {
                $event = DB::table('events')->where('id', $tenant->active_event_id)->first();

                if ($event === null) {
                    return;
                }

                DB::table('tenants')->where('id', $tenant->id)->update([
                    'event_title' => $event->name,
                    'event_location' => $event->address,
                ]);
            });

        DB::table('events')->orderBy('id')->each(function (object $event): void {
            DB::table('event_dates')
                ->where('event_id', $event->id)
                ->update(['tenant_code' => $event->tenant_code]);
        });

        Schema::table('tenants', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('active_event_id');
        });
        Schema::table('catalog_items', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('event_id');
        });
        Schema::table('compras', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('event_id');
        });
        Schema::table('event_dates', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('event_id');
            $table->string('tenant_code')->nullable(false)->change();
            $table->foreign('tenant_code')
                ->references('codigo')
                ->on('tenants')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();
        });

        Schema::dropIfExists('events');
    }

    public function down(): void
    {
        Schema::create('events', function (Blueprint $table): void {
            $table->id();
            $table->string('tenant_code');
            $table->string('name');
            $table->string('address');
            $table->foreign('tenant_code')
                ->references('codigo')
                ->on('tenants')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();
        });

        Schema::table('tenants', function (Blueprint $table): void {
            $table->foreignId('active_event_id')->nullable();
        });
        Schema::table('catalog_items', function (Blueprint $table): void {
            $table->foreignId('event_id')->nullable();
        });
        Schema::table('compras', function (Blueprint $table): void {
            $table->foreignId('event_id')->nullable();
        });
        Schema::table('event_dates', function (Blueprint $table): void {
            $table->foreignId('event_id')->nullable();
        });

        DB::table('tenants')->orderBy('id')->each(function (object $tenant): void {
            if ($tenant->event_title === null && $tenant->event_location === null) {
                return;
            }

            $eventId = DB::table('events')->insertGetId([
                'tenant_code' => $tenant->codigo,
                'name' => $tenant->event_title ?? $tenant->nombre,
                'address' => $tenant->event_location ?? '',
            ]);

            DB::table('tenants')->where('id', $tenant->id)->update(['active_event_id' => $eventId]);
            DB::table('catalog_items')->where('tenant_code', $tenant->codigo)->update(['event_id' => $eventId]);
            DB::table('compras')->where('tenant_codigo', $tenant->codigo)->update(['event_id' => $eventId]);
            DB::table('event_dates')->where('tenant_code', $tenant->codigo)->update(['event_id' => $eventId]);
        });

        Schema::table('tenants', function (Blueprint $table): void {
            $table->foreign('active_event_id')->references('id')->on('events')->nullOnDelete();
            $table->dropColumn(['event_title', 'event_location']);
        });
        Schema::table('catalog_items', function (Blueprint $table): void {
            $table->foreign('event_id')->references('id')->on('events')->nullOnDelete();
        });
        Schema::table('compras', function (Blueprint $table): void {
            $table->foreign('event_id')->references('id')->on('events')->nullOnDelete();
        });
        Schema::table('event_dates', function (Blueprint $table): void {
            $table->dropForeign(['tenant_code']);
            $table->dropColumn('tenant_code');
            $table->foreign('event_id')->references('id')->on('events')->cascadeOnDelete();
        });
    }
};
