<?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('item_attributes', function (Blueprint $table): void {
            $table->string('ticket_label', 100)->nullable()->after('show_in_selector');
        });

        foreach (['sector' => 'Lado', 'fila' => 'Fila', 'asiento' => 'Asiento'] as $code => $label) {
            $itemAttributeIds = DB::table('item_attributes')
                ->join('catalog_items', 'catalog_items.id', '=', 'item_attributes.catalog_item_id')
                ->join('attribute', 'attribute.id', '=', 'item_attributes.attribute_id')
                ->where('catalog_items.tenant_code', 'desfile_pura_tendencia')
                ->where('attribute.codigo', $code)
                ->pluck('item_attributes.id');

            DB::table('item_attributes')
                ->whereIn('id', $itemAttributeIds)
                ->update(['ticket_label' => $label]);
        }
    }

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