import { MigrationInterface, QueryRunner } from 'typeorm';

export class addPromotionPlans1687369901561 implements MigrationInterface {
  name = 'addPromotionPlans1687369901561';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE \`discounts\` 
DROP COLUMN \`company\`,
ADD COLUMN \`userId\` CHAR(36) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL AFTER \`name\`,
ADD COLUMN \`customerId\` INT(11) NULL DEFAULT NULL AFTER \`userId\`,
ADD INDEX \`fk_discounts_customers1_idx\` (\`customerId\` ASC),
ADD INDEX \`fk_discounts_users1_idx\` (\`userId\` ASC);`);

    await queryRunner.query(
      `ALTER TABLE \`users\` ADD COLUMN \`birthday\` DATE NULL DEFAULT NULL AFTER \`gender\`;`,
    );

    await queryRunner.query(`CREATE TABLE IF NOT EXISTS \`promotion_plans\` (
  \`id\` INT(11) NOT NULL AUTO_INCREMENT,
  \`name\` VARCHAR(255) NOT NULL,
  \`type\` ENUM('signup_discount', 'birthday_discount') NOT NULL,
  \`customerId\` INT(11) NULL DEFAULT NULL,
  \`validityDays\` INT(11) NULL DEFAULT NULL,
  \`usePercentage\` TINYINT(1) NULL DEFAULT NULL,
  \`discountPercentage\` INT(11) NULL DEFAULT NULL,
  \`discountAmount\` INT(11) NULL DEFAULT NULL,
  \`startDate\` DATETIME NULL DEFAULT NULL,
  \`endDate\` DATETIME NULL DEFAULT NULL,
  \`section\` ENUM('product', 'service', 'both') NOT NULL DEFAULT 'service',
  \`createdAt\` DATETIME NOT NULL,
  \`updatedAt\` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (\`id\`),
  INDEX \`fk_promotion_plans_customers1_idx\` (\`customerId\` ASC),
  CONSTRAINT \`fk_promotion_plans_customers1\`
    FOREIGN KEY (\`customerId\`)
    REFERENCES \`customers\` (\`id\`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;`);

    await queryRunner.query(`ALTER TABLE \`discounts\` 
ADD CONSTRAINT \`fk_discounts_customers1\`
  FOREIGN KEY (\`customerId\`)
  REFERENCES \`customers\` (\`id\`)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION,
ADD CONSTRAINT \`fk_discounts_users1\`
  FOREIGN KEY (\`userId\`)
  REFERENCES \`users\` (\`id\`)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION;`);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    //
  }
}
