import { QueryInterface, DataTypes } from 'sequelize';

/**
 * Migration to create activity_types table for claim management activity types
 * Admin can manage activity types similar to holiday management
 */
export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.createTable('activity_types', {
    activity_type_id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true
    },
    title: {
      type: DataTypes.STRING(200),
      allowNull: false,
      unique: true,
      comment: 'Activity type title/name (e.g., "Riders Mania Claims", "Legal Claims Reimbursement")'
    },
    item_code: {
      type: DataTypes.STRING(100),
      allowNull: true,
      defaultValue: null,
      comment: 'Optional item code for the activity type'
    },
    taxation_type: {
      type: DataTypes.STRING(100),
      allowNull: true,
      defaultValue: null,
      comment: 'Optional taxation type for the activity'
    },
    sap_ref_no: {
      type: DataTypes.STRING(100),
      allowNull: true,
      defaultValue: null,
      comment: 'Optional SAP reference number'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      comment: 'Whether this activity type is currently active/available for selection'
    },
    created_by: {
      type: DataTypes.UUID,
      allowNull: false,
      references: {
        model: 'users',
        key: 'user_id'
      },
      comment: 'Admin user who created this activity type'
    },
    updated_by: {
      type: DataTypes.UUID,
      allowNull: true,
      references: {
        model: 'users',
        key: 'user_id'
      },
      comment: 'Admin user who last updated this activity type'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  });

  // Indexes for performance
  await queryInterface.sequelize.query('CREATE UNIQUE INDEX IF NOT EXISTS "activity_types_title_unique" ON "activity_types" ("title");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "activity_types_is_active" ON "activity_types" ("is_active");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "activity_types_item_code" ON "activity_types" ("item_code");');
  await queryInterface.sequelize.query('CREATE INDEX IF NOT EXISTS "activity_types_created_by" ON "activity_types" ("created_by");');
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('activity_types');
}

