<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::rename('carts', 'service_packages');
        Schema::rename('cart_items', 'service_package_items');

        Schema::table('service_package_items', function ($table) {
            $table->renameColumn('cart_id', 'service_package_id');
        });

        Schema::table('payments', function ($table) {
            $table->renameColumn('cart_id', 'service_package_id');
        });

        DB::statement('ALTER SEQUENCE IF EXISTS carts_id_seq RENAME TO service_packages_id_seq');
        DB::statement('ALTER SEQUENCE IF EXISTS cart_items_id_seq RENAME TO service_package_items_id_seq');

        DB::statement('ALTER TABLE service_packages RENAME CONSTRAINT carts_pkey TO service_packages_pkey');
        DB::statement('ALTER TABLE service_packages RENAME CONSTRAINT carts_client_id_foreign TO service_packages_client_id_foreign');
        DB::statement('ALTER TABLE service_packages RENAME CONSTRAINT carts_provider_id_foreign TO service_packages_provider_id_foreign');

        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT cart_items_pkey TO service_package_items_pkey');
        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT cart_items_cart_id_foreign TO service_package_items_service_package_id_foreign');
        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT cart_items_schedule_id_foreign TO service_package_items_schedule_id_foreign');
        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT cart_items_cart_id_schedule_id_unique TO service_package_items_service_package_id_schedule_id_unique');

        DB::statement('ALTER TABLE payments RENAME CONSTRAINT payments_cart_id_foreign TO payments_service_package_id_foreign');
        DB::statement('ALTER TABLE payments RENAME CONSTRAINT payments_exactly_one_payable_check TO payments_exactly_one_payable_service_package_check');

        DB::statement('ALTER INDEX IF EXISTS carts_client_id_index RENAME TO service_packages_client_id_index');
        DB::statement('ALTER INDEX IF EXISTS carts_provider_id_index RENAME TO service_packages_provider_id_index');
        DB::statement('ALTER INDEX IF EXISTS cart_items_cart_id_index RENAME TO service_package_items_service_package_id_index');
        DB::statement('ALTER INDEX IF EXISTS cart_items_schedule_id_index RENAME TO service_package_items_schedule_id_index');
        DB::statement('ALTER INDEX IF EXISTS cart_items_one_cart_per_schedule RENAME TO service_package_items_one_package_per_schedule');
        DB::statement('ALTER INDEX IF EXISTS payments_cart_id_index RENAME TO payments_service_package_id_index');
        DB::statement('ALTER INDEX IF EXISTS payments_one_active_per_cart RENAME TO payments_one_active_per_service_package');

        DB::table('permissions')->where('scope', 'cart')->update([
            'scope'       => 'service-package',
            'description' => 'Pacotes de serviços',
        ]);

        DB::table('permissions')->where('scope', 'cart-item')->update([
            'scope'       => 'service-package-item',
            'description' => 'Itens de pacotes de serviços',
        ]);
    }

    public function down(): void
    {
        DB::table('permissions')->where('scope', 'service-package')->update([
            'scope'       => 'cart',
            'description' => 'Carrinhos',
        ]);

        DB::table('permissions')->where('scope', 'service-package-item')->update([
            'scope'       => 'cart-item',
            'description' => 'Itens de carrinhos',
        ]);

        DB::statement('ALTER INDEX IF EXISTS service_packages_client_id_index RENAME TO carts_client_id_index');
        DB::statement('ALTER INDEX IF EXISTS service_packages_provider_id_index RENAME TO carts_provider_id_index');
        DB::statement('ALTER INDEX IF EXISTS service_package_items_service_package_id_index RENAME TO cart_items_cart_id_index');
        DB::statement('ALTER INDEX IF EXISTS service_package_items_schedule_id_index RENAME TO cart_items_schedule_id_index');
        DB::statement('ALTER INDEX IF EXISTS service_package_items_one_package_per_schedule RENAME TO cart_items_one_cart_per_schedule');
        DB::statement('ALTER INDEX IF EXISTS payments_service_package_id_index RENAME TO payments_cart_id_index');
        DB::statement('ALTER INDEX IF EXISTS payments_one_active_per_service_package RENAME TO payments_one_active_per_cart');

        DB::statement('ALTER TABLE service_packages RENAME CONSTRAINT service_packages_pkey TO carts_pkey');
        DB::statement('ALTER TABLE service_packages RENAME CONSTRAINT service_packages_client_id_foreign TO carts_client_id_foreign');
        DB::statement('ALTER TABLE service_packages RENAME CONSTRAINT service_packages_provider_id_foreign TO carts_provider_id_foreign');

        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT service_package_items_pkey TO cart_items_pkey');
        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT service_package_items_service_package_id_foreign TO cart_items_cart_id_foreign');
        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT service_package_items_schedule_id_foreign TO cart_items_schedule_id_foreign');
        DB::statement('ALTER TABLE service_package_items RENAME CONSTRAINT service_package_items_service_package_id_schedule_id_unique TO cart_items_cart_id_schedule_id_unique');

        DB::statement('ALTER TABLE payments RENAME CONSTRAINT payments_service_package_id_foreign TO payments_cart_id_foreign');
        DB::statement('ALTER TABLE payments RENAME CONSTRAINT payments_exactly_one_payable_service_package_check TO payments_exactly_one_payable_check');

        Schema::table('service_package_items', function ($table) {
            $table->renameColumn('service_package_id', 'cart_id');
        });

        Schema::table('payments', function ($table) {
            $table->renameColumn('service_package_id', 'cart_id');
        });

        DB::statement('ALTER SEQUENCE IF EXISTS service_packages_id_seq RENAME TO carts_id_seq');
        DB::statement('ALTER SEQUENCE IF EXISTS service_package_items_id_seq RENAME TO cart_items_id_seq');

        Schema::rename('service_package_items', 'cart_items');
        Schema::rename('service_packages', 'carts');
    }
};
