<?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
{
    /** @var array<string, string> */
    private const TENANT_POLICIES = [
        'sonder' => 'quantity_and_remove',
        'fiesta_futbol_infantil' => 'full',
        'desfile_pura_tendencia' => 'disabled',
    ];

    public function up(): void
    {
        Schema::table('tenants', function (Blueprint $table): void {
            $table->string('cart_editing_policy')
                ->default('full')
                ->after('display_cart');
        });

        DB::table('tenants')
            ->where('cart_editing_enabled', false)
            ->update(['cart_editing_policy' => 'disabled']);

        foreach (self::TENANT_POLICIES as $tenantCode => $policy) {
            DB::table('tenants')
                ->where('codigo', $tenantCode)
                ->update(['cart_editing_policy' => $policy]);
        }

        Schema::table('tenants', function (Blueprint $table): void {
            $table->dropColumn('cart_editing_enabled');
        });
    }

    public function down(): void
    {
        Schema::table('tenants', function (Blueprint $table): void {
            $table->boolean('cart_editing_enabled')
                ->default(true)
                ->after('display_cart');
        });

        DB::table('tenants')
            ->where('cart_editing_policy', 'disabled')
            ->update(['cart_editing_enabled' => false]);

        Schema::table('tenants', function (Blueprint $table): void {
            $table->dropColumn('cart_editing_policy');
        });
    }
};
