<?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('catalog_items', function (Blueprint $table): void {
            $table->unsignedInteger('group_order')->default(0)->after('nombre');
        });

        $footballOrder = [
            1 => [
                'slugs' => ['camiseta', 'camiseta-oficial-fnfi'],
                'names' => ['Camiseta', 'CAMISETA OFICIAL FNFI'],
            ],
            2 => [
                'slugs' => ['alojamiento', 'camping'],
                'names' => ['Alojamiento', 'CAMPING'],
            ],
            3 => [
                'slugs' => ['abono'],
                'names' => ['Abono', 'ABONO'],
            ],
            4 => [
                'slugs' => ['comida'],
                'names' => ['Comida', 'COMIDA'],
            ],
        ];

        foreach ($footballOrder as $order => $identifiers) {
            DB::table('catalog_items')
                ->where('tenant_code', 'fiesta_futbol_infantil')
                ->where(function ($query) use ($identifiers): void {
                    $query
                        ->whereIn('slug', $identifiers['slugs'])
                        ->orWhereIn('nombre', $identifiers['names']);
                })
                ->update(['group_order' => $order]);
        }
    }

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