import { QueryInterface, DataTypes } from 'sequelize';

/**
 * Migration to create holidays table for organization holiday calendar
 * Holidays are excluded from working days in TAT calculations for STANDARD priority
 */
export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.createTable('holidays', {
    holiday_id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true
    },
    holiday_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      unique: true,
      comment: 'The date of the holiday (YYYY-MM-DD)'
    },
    holiday_name: {
      type: DataTypes.STRING(200),
      allowNull: false,
      comment: 'Name/title of the holiday (e.g., "Diwali", "Republic Day")'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Optional description or notes about the holiday'
    },
    is_recurring: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      comment: 'Whether this holiday recurs annually (e.g., Independence Day)'
    },
    recurrence_rule: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'RRULE for recurring holidays (e.g., "FREQ=YEARLY;BYMONTH=8;BYMONTHDAY=15")'
    },
    holiday_type: {
      type: DataTypes.ENUM('NATIONAL', 'REGIONAL', 'ORGANIZATIONAL', 'OPTIONAL'),
      defaultValue: 'ORGANIZATIONAL',
      comment: 'Type of holiday'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      comment: 'Whether this holiday is currently active/applicable'
    },
    applies_to_departments: {
      type: DataTypes.ARRAY(DataTypes.STRING),
      allowNull: true,
      defaultValue: null,
      comment: 'If null, applies to all departments. Otherwise, specific departments only'
    },
    applies_to_locations: {
      type: DataTypes.ARRAY(DataTypes.STRING),
      allowNull: true,
      defaultValue: null,
      comment: 'If null, applies to all locations. Otherwise, specific locations only'
    },
    created_by: {
      type: DataTypes.UUID,
      allowNull: false,
      references: {
        model: 'users',
        key: 'user_id'
      },
      comment: 'Admin user who created this holiday'
    },
    updated_by: {
      type: DataTypes.UUID,
      allowNull: true,
      references: {
        model: 'users',
        key: 'user_id'
      },
      comment: 'Admin user who last updated this holiday'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  });

  // Indexes for performance (with IF NOT EXISTS)
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "holidays_holiday_date" ON "holidays" ("holiday_date");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "holidays_is_active" ON "holidays" ("is_active");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "holidays_holiday_type" ON "holidays" ("holiday_type");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "holidays_created_by" ON "holidays" ("created_by");');

  // Holidays table created
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('holidays');
  await queryInterface.sequelize.query('DROP TYPE IF EXISTS "enum_holidays_holiday_type";');
  // Holidays table dropped
}

