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

export enum TatAlertType {
  TAT_50 = 'TAT_50',
  TAT_75 = 'TAT_75',
  TAT_100 = 'TAT_100'
}

interface TatAlertAttributes {
  alertId: string;
  requestId: string;
  levelId: string;
  approverId: string;
  alertType: TatAlertType;
  thresholdPercentage: number;
  tatHoursAllocated: number;
  tatHoursElapsed: number;
  tatHoursRemaining: number;
  levelStartTime: Date;
  alertSentAt: Date;
  expectedCompletionTime: Date;
  alertMessage: string;
  notificationSent: boolean;
  notificationChannels: string[];
  isBreached: boolean;
  wasCompletedOnTime?: boolean;
  completionTime?: Date;
  metadata: Record<string, any>;
  createdAt: Date;
}

interface TatAlertCreationAttributes extends Optional<TatAlertAttributes, 'alertId' | 'notificationSent' | 'notificationChannels' | 'isBreached' | 'wasCompletedOnTime' | 'completionTime' | 'metadata' | 'alertSentAt' | 'createdAt'> {}

class TatAlert extends Model<TatAlertAttributes, TatAlertCreationAttributes> implements TatAlertAttributes {
  public alertId!: string;
  public requestId!: string;
  public levelId!: string;
  public approverId!: string;
  public alertType!: TatAlertType;
  public thresholdPercentage!: number;
  public tatHoursAllocated!: number;
  public tatHoursElapsed!: number;
  public tatHoursRemaining!: number;
  public levelStartTime!: Date;
  public alertSentAt!: Date;
  public expectedCompletionTime!: Date;
  public alertMessage!: string;
  public notificationSent!: boolean;
  public notificationChannels!: string[];
  public isBreached!: boolean;
  public wasCompletedOnTime?: boolean;
  public completionTime?: Date;
  public metadata!: Record<string, any>;
  public createdAt!: Date;

  // Associations
  public request?: WorkflowRequest;
  public level?: ApprovalLevel;
  public approver?: User;
}

TatAlert.init(
  {
    alertId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'alert_id'
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'request_id'
    },
    levelId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'level_id'
    },
    approverId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'approver_id'
    },
    alertType: {
      type: DataTypes.ENUM('TAT_50', 'TAT_75', 'TAT_100'),
      allowNull: false,
      field: 'alert_type'
    },
    thresholdPercentage: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'threshold_percentage'
    },
    tatHoursAllocated: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      field: 'tat_hours_allocated'
    },
    tatHoursElapsed: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      field: 'tat_hours_elapsed'
    },
    tatHoursRemaining: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      field: 'tat_hours_remaining'
    },
    levelStartTime: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'level_start_time'
    },
    alertSentAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'alert_sent_at'
    },
    expectedCompletionTime: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'expected_completion_time'
    },
    alertMessage: {
      type: DataTypes.TEXT,
      allowNull: false,
      field: 'alert_message'
    },
    notificationSent: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'notification_sent'
    },
    notificationChannels: {
      type: DataTypes.ARRAY(DataTypes.STRING),
      defaultValue: [],
      field: 'notification_channels'
    },
    isBreached: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'is_breached'
    },
    wasCompletedOnTime: {
      type: DataTypes.BOOLEAN,
      allowNull: true,
      field: 'was_completed_on_time'
    },
    completionTime: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'completion_time'
    },
    metadata: {
      type: DataTypes.JSONB,
      defaultValue: {},
      field: 'metadata'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    }
  },
  {
    sequelize,
    modelName: 'TatAlert',
    tableName: 'tat_alerts',
    timestamps: false,
    indexes: [
      { fields: ['request_id'] },
      { fields: ['level_id'] },
      { fields: ['approver_id'] },
      { fields: ['alert_type'] },
      { fields: ['alert_sent_at'] },
      { fields: ['is_breached'] },
      { fields: ['was_completed_on_time'] }
    ]
  }
);

// Associations
TatAlert.belongsTo(WorkflowRequest, {
  as: 'request',
  foreignKey: 'requestId',
  targetKey: 'requestId'
});

TatAlert.belongsTo(ApprovalLevel, {
  as: 'level',
  foreignKey: 'levelId',
  targetKey: 'levelId'
});

TatAlert.belongsTo(User, {
  as: 'approver',
  foreignKey: 'approverId',
  targetKey: 'userId'
});

export { TatAlert };

