import { Migration } from '@mikro-orm/migrations';

export class Migration20260603115026 extends Migration {

  override async up(): Promise<void> {
    this.addSql(`alter table "point_transactions" drop constraint if exists "point_transactions_type_check";`);

    this.addSql(`drop index "restaurants_is_active_index";`);

    this.addSql(`create index "restaurants_slug_index" on "restaurants" ("slug");`);

    this.addSql(`create index "payment_methods_restaurant_id_index" on "payment_methods" ("restaurant_id");`);

    this.addSql(`create index "categories_restaurant_id_id_index" on "categories" ("restaurant_id", "id");`);

    this.addSql(`drop index "foods_category_id_is_active_index";`);
    this.addSql(`drop index "foods_is_active_index";`);

    this.addSql(`alter table "admin_roles" drop constraint "admin_roles_admin_id_restaurant_id_unique";`);

    this.addSql(`create index "schedules_rest_id_index" on "schedules" ("rest_id");`);

    this.addSql(`drop index "users_is_active_index";`);

    this.addSql(`create index "users_phone_index" on "users" ("phone");`);

    this.addSql(`drop index "point_transactions_restaurant_id_index";`);
    this.addSql(`drop index "point_transactions_user_id_index";`);

    this.addSql(`alter table "point_transactions" add constraint "point_transactions_type_check" check("type" in (''));`);

    this.addSql(`create index "pagers_restaurant_id_index" on "pagers" ("restaurant_id");`);

    this.addSql(`alter table "orders" drop constraint "orders_restaurant_id_order_number_unique";`);
    this.addSql(`drop index "orders_status_index";`);
    this.addSql(`drop index "orders_user_id_status_index";`);

    this.addSql(`create index "orders_restaurant_id_index" on "orders" ("restaurant_id");`);

    this.addSql(`drop index "order_items_food_id_index";`);

    this.addSql(`create index "user_addresses_user_id_index" on "user_addresses" ("user_id");`);

    this.addSql(`drop index "wallet_transactions_restaurant_id_index";`);
    this.addSql(`drop index "wallet_transactions_user_id_index";`);
  }

  override async down(): Promise<void> {
    this.addSql(`create table "user_wallets" ("id" char(26) not null, "created_at" timestamptz(6) not null default now(), "updated_at" timestamptz(6) not null default now(), "deleted_at" timestamptz(6) null, "restaurant_id" char(26) not null, "user_id" char(26) not null, "wallet" int4 not null default 0, "points" int4 not null default 0, constraint "user_wallets_pkey" primary key ("id"));`);
    this.addSql(`alter table "user_wallets" add constraint "unique_user_restaurant" unique ("user_id", "restaurant_id");`);
    this.addSql(`create index "user_wallets_created_at_index" on "user_wallets" ("created_at");`);
    this.addSql(`create index "user_wallets_deleted_at_index" on "user_wallets" ("deleted_at");`);
    this.addSql(`create index "user_wallets_restaurant_id_index" on "user_wallets" ("restaurant_id");`);
    this.addSql(`create index "user_wallets_user_id_index" on "user_wallets" ("user_id");`);
    this.addSql(`create index "user_wallets_user_id_restaurant_id_index" on "user_wallets" ("user_id", "restaurant_id");`);

    this.addSql(`alter table "point_transactions" drop constraint if exists "point_transactions_type_check";`);

    this.addSql(`alter table "admin_roles" add constraint "admin_roles_admin_id_restaurant_id_unique" unique ("admin_id", "restaurant_id");`);

    this.addSql(`drop index "categories_restaurant_id_id_index";`);

    this.addSql(`create index "foods_category_id_is_active_index" on "foods" ("category_id", "is_active");`);
    this.addSql(`create index "foods_is_active_index" on "foods" ("is_active");`);

    this.addSql(`create index "order_items_food_id_index" on "order_items" ("food_id");`);

    this.addSql(`drop index "orders_restaurant_id_index";`);

    this.addSql(`alter table "orders" add constraint "orders_restaurant_id_order_number_unique" unique ("restaurant_id", "order_number");`);
    this.addSql(`create index "orders_status_index" on "orders" ("status");`);
    this.addSql(`create index "orders_user_id_status_index" on "orders" ("user_id", "status");`);

    this.addSql(`drop index "pagers_restaurant_id_index";`);

    this.addSql(`drop index "payment_methods_restaurant_id_index";`);

    this.addSql(`alter table "point_transactions" add constraint "point_transactions_type_check" check("type" in (''));`);
    this.addSql(`create index "point_transactions_restaurant_id_index" on "point_transactions" ("restaurant_id");`);
    this.addSql(`create index "point_transactions_user_id_index" on "point_transactions" ("user_id");`);

    this.addSql(`drop index "restaurants_slug_index";`);

    this.addSql(`create index "restaurants_is_active_index" on "restaurants" ("is_active");`);

    this.addSql(`drop index "schedules_rest_id_index";`);

    this.addSql(`drop index "user_addresses_user_id_index";`);

    this.addSql(`drop index "users_phone_index";`);

    this.addSql(`create index "users_is_active_index" on "users" ("is_active");`);

    this.addSql(`create index "wallet_transactions_restaurant_id_index" on "wallet_transactions" ("restaurant_id");`);
    this.addSql(`create index "wallet_transactions_user_id_index" on "wallet_transactions" ("user_id");`);
  }

}
