<?php

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

return new class extends Migration
{
    private const CLIENT_CODE = 'onticket';

    private const TENANT_CODES = [
        'fiesta_futbol_infantil',
        'desfile_pura_tendencia',
    ];

    public function up(): void
    {
        $now = now();

        $clientId = DB::table('clients')->where('code', self::CLIENT_CODE)->value('id');

        if ($clientId === null) {
            $clientId = DB::table('clients')->insertGetId([
                'code' => self::CLIENT_CODE,
                'name' => 'OnTicket',
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        } else {
            DB::table('clients')->where('id', $clientId)->update([
                'name' => 'OnTicket',
                'updated_at' => $now,
            ]);
        }

        $tenants = DB::table('tenants')
            ->whereIn('codigo', self::TENANT_CODES)
            ->orderByRaw("CASE codigo WHEN 'fiesta_futbol_infantil' THEN 0 ELSE 1 END")
            ->get(['codigo', 'client_id']);

        foreach ($tenants as $tenant) {
            DB::table('client_integrations')
                ->where('client_id', $tenant->client_id)
                ->orderBy('id')
                ->get()
                ->each(function (object $integration) use ($clientId): void {
                    DB::table('client_integrations')->insertOrIgnore([
                        'client_id' => $clientId,
                        'integration_code' => $integration->integration_code,
                        'integration_data' => $integration->integration_data,
                        'created_at' => $integration->created_at,
                        'updated_at' => $integration->updated_at,
                    ]);
                });
        }

        DB::table('tenants')
            ->whereIn('codigo', self::TENANT_CODES)
            ->update(['client_id' => $clientId]);
    }

    public function down(): void
    {
        foreach (self::TENANT_CODES as $tenantCode) {
            $tenant = DB::table('tenants')->where('codigo', $tenantCode)->first(['id']);

            if (! $tenant) {
                continue;
            }

            DB::table('clients')->insertOrIgnore([
                'code' => $tenantCode,
                'name' => $tenantCode === 'fiesta_futbol_infantil'
                    ? 'Fiesta Fútbol Infantil'
                    : 'Desfile Pura Tendencia',
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            $individualClientId = DB::table('clients')->where('code', $tenantCode)->value('id');
            $sharedClientId = DB::table('clients')->where('code', self::CLIENT_CODE)->value('id');

            DB::table('client_integrations')
                ->where('client_id', $sharedClientId)
                ->orderBy('id')
                ->get()
                ->each(function (object $integration) use ($individualClientId): void {
                    DB::table('client_integrations')->insertOrIgnore([
                        'client_id' => $individualClientId,
                        'integration_code' => $integration->integration_code,
                        'integration_data' => $integration->integration_data,
                        'created_at' => $integration->created_at,
                        'updated_at' => $integration->updated_at,
                    ]);
                });

            DB::table('tenants')->where('id', $tenant->id)->update([
                'client_id' => $individualClientId,
            ]);
        }
    }
};
