﻿import { MigrationInterface, QueryRunner, Table, TableIndex } from "typeorm";

export class AddUsersPermissionsVehicularCatalogs1740152000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE taller_usuarios MODIFY COLUMN rol ENUM('super_admin','admin','operativo','recepcion','mecanico') NOT NULL DEFAULT 'operativo'`,
    );
    await queryRunner.query(
      `ALTER TABLE taller_usuarios ADD COLUMN permisos JSON NULL AFTER rol`,
    );

    await queryRunner.createTable(
      new Table({
        name: "taller_catalogos_vehiculares",
        columns: [
          {
            name: "id",
            type: "int",
            isPrimary: true,
            isGenerated: true,
            generationStrategy: "increment",
          },
          {
            name: "tipo",
            type: "enum",
            enum: ["marca", "modelo", "motor", "cilindraje"],
          },
          { name: "nombre", type: "varchar", length: "120" },
          { name: "marca", type: "varchar", length: "120", isNullable: true },
          { name: "modelo", type: "varchar", length: "120", isNullable: true },
          { name: "motor", type: "varchar", length: "120", isNullable: true },
          { name: "activo", type: "boolean", default: true },
          {
            name: "created_at",
            type: "timestamp",
            default: "CURRENT_TIMESTAMP",
          },
          {
            name: "updated_at",
            type: "timestamp",
            default: "CURRENT_TIMESTAMP",
            onUpdate: "CURRENT_TIMESTAMP",
          },
        ],
      }),
      true,
    );

    await queryRunner.createIndex(
      "taller_catalogos_vehiculares",
      new TableIndex({ name: "IDX_cat_veh_tipo", columnNames: ["tipo"] }),
    );
    await queryRunner.createIndex(
      "taller_catalogos_vehiculares",
      new TableIndex({ name: "IDX_cat_veh_nombre", columnNames: ["nombre"] }),
    );
    await queryRunner.createIndex(
      "taller_catalogos_vehiculares",
      new TableIndex({ name: "IDX_cat_veh_activo", columnNames: ["activo"] }),
    );

    await queryRunner.createTable(
      new Table({
        name: "taller_clientes_vehiculos",
        columns: [
          {
            name: "id",
            type: "int",
            isPrimary: true,
            isGenerated: true,
            generationStrategy: "increment",
          },
          { name: "vehiculo_id", type: "int" },
          { name: "cliente_tarjeta", type: "varchar", length: "24" },
        ],
        uniques: [
          {
            name: "UQ_cliente_vehiculo",
            columnNames: ["vehiculo_id", "cliente_tarjeta"],
          },
        ],
        foreignKeys: [
          {
            columnNames: ["vehiculo_id"],
            referencedTableName: "taller_vehiculos",
            referencedColumnNames: ["id"],
            onDelete: "CASCADE",
          },
        ],
      }),
      true,
    );

    await queryRunner.createIndex(
      "taller_clientes_vehiculos",
      new TableIndex({
        name: "IDX_cliente_vehiculo_vehiculo",
        columnNames: ["vehiculo_id"],
      }),
    );
    await queryRunner.createIndex(
      "taller_clientes_vehiculos",
      new TableIndex({
        name: "IDX_cliente_vehiculo_cliente",
        columnNames: ["cliente_tarjeta"],
      }),
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable("taller_clientes_vehiculos", true);
    await queryRunner.dropTable("taller_catalogos_vehiculares", true);
    await queryRunner.query(`ALTER TABLE taller_usuarios DROP COLUMN permisos`);
    await queryRunner.query(
      `ALTER TABLE taller_usuarios MODIFY COLUMN rol ENUM('admin','recepcion','mecanico') NOT NULL DEFAULT 'recepcion'`,
    );
  }
}
