import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';
import { User } from './User';

export enum HolidayType {
  NATIONAL = 'NATIONAL',
  REGIONAL = 'REGIONAL',
  ORGANIZATIONAL = 'ORGANIZATIONAL',
  OPTIONAL = 'OPTIONAL'
}

interface HolidayAttributes {
  holidayId: string;
  holidayDate: string; // YYYY-MM-DD format
  holidayName: string;
  description?: string;
  isRecurring: boolean;
  recurrenceRule?: string;
  holidayType: HolidayType;
  isActive: boolean;
  appliesToDepartments?: string[];
  appliesToLocations?: string[];
  createdBy: string;
  updatedBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

interface HolidayCreationAttributes extends Optional<HolidayAttributes, 'holidayId' | 'description' | 'isRecurring' | 'recurrenceRule' | 'holidayType' | 'isActive' | 'appliesToDepartments' | 'appliesToLocations' | 'updatedBy' | 'createdAt' | 'updatedAt'> {}

class Holiday extends Model<HolidayAttributes, HolidayCreationAttributes> implements HolidayAttributes {
  public holidayId!: string;
  public holidayDate!: string;
  public holidayName!: string;
  public description?: string;
  public isRecurring!: boolean;
  public recurrenceRule?: string;
  public holidayType!: HolidayType;
  public isActive!: boolean;
  public appliesToDepartments?: string[];
  public appliesToLocations?: string[];
  public createdBy!: string;
  public updatedBy?: string;
  public createdAt!: Date;
  public updatedAt!: Date;

  // Associations
  public creator?: User;
  public updater?: User;
}

Holiday.init(
  {
    holidayId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'holiday_id'
    },
    holidayDate: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      unique: true,
      field: 'holiday_date'
    },
    holidayName: {
      type: DataTypes.STRING(200),
      allowNull: false,
      field: 'holiday_name'
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'description'
    },
    isRecurring: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'is_recurring'
    },
    recurrenceRule: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'recurrence_rule'
    },
    holidayType: {
      type: DataTypes.ENUM('NATIONAL', 'REGIONAL', 'ORGANIZATIONAL', 'OPTIONAL'),
      defaultValue: 'ORGANIZATIONAL',
      field: 'holiday_type'
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'is_active'
    },
    appliesToDepartments: {
      type: DataTypes.ARRAY(DataTypes.STRING),
      allowNull: true,
      defaultValue: null,
      field: 'applies_to_departments'
    },
    appliesToLocations: {
      type: DataTypes.ARRAY(DataTypes.STRING),
      allowNull: true,
      defaultValue: null,
      field: 'applies_to_locations'
    },
    createdBy: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'created_by'
    },
    updatedBy: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'updated_by'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'updated_at'
    }
  },
  {
    sequelize,
    modelName: 'Holiday',
    tableName: 'holidays',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['holiday_date'] },
      { fields: ['is_active'] },
      { fields: ['holiday_type'] },
      { fields: ['created_by'] }
    ]
  }
);

// Associations
Holiday.belongsTo(User, {
  as: 'creator',
  foreignKey: 'createdBy',
  targetKey: 'userId'
});

Holiday.belongsTo(User, {
  as: 'updater',
  foreignKey: 'updatedBy',
  targetKey: 'userId'
});

export { Holiday };

