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

interface NotificationAttributes {
  notificationId: string;
  userId: string;
  requestId?: string;
  notificationType: string;
  title: string;
  message: string;
  isRead: boolean;
  priority: 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT';
  actionUrl?: string;
  actionRequired: boolean;
  metadata?: any;
  sentVia: string[];
  emailSent: boolean;
  smsSent: boolean;
  pushSent: boolean;
  readAt?: Date;
  expiresAt?: Date;
  createdAt: Date;
}

interface NotificationCreationAttributes extends Optional<NotificationAttributes, 'notificationId' | 'isRead' | 'priority' | 'actionRequired' | 'sentVia' | 'emailSent' | 'smsSent' | 'pushSent' | 'createdAt'> {}

class Notification extends Model<NotificationAttributes, NotificationCreationAttributes> implements NotificationAttributes {
  public notificationId!: string;
  public userId!: string;
  public requestId?: string;
  public notificationType!: string;
  public title!: string;
  public message!: string;
  public isRead!: boolean;
  public priority!: 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT';
  public actionUrl?: string;
  public actionRequired!: boolean;
  public metadata?: any;
  public sentVia!: string[];
  public emailSent!: boolean;
  public smsSent!: boolean;
  public pushSent!: boolean;
  public readAt?: Date;
  public expiresAt?: Date;
  public readonly createdAt!: Date;
}

Notification.init(
  {
    notificationId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'notification_id'
    },
    userId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'user_id',
      references: {
        model: 'users',
        key: 'user_id'
      }
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id'
      }
    },
    notificationType: {
      type: DataTypes.STRING(50),
      allowNull: false,
      field: 'notification_type'
    },
    title: {
      type: DataTypes.STRING(255),
      allowNull: false
    },
    message: {
      type: DataTypes.TEXT,
      allowNull: false
    },
    isRead: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'is_read'
    },
    priority: {
      type: DataTypes.ENUM('LOW', 'MEDIUM', 'HIGH', 'URGENT'),
      defaultValue: 'MEDIUM'
    },
    actionUrl: {
      type: DataTypes.STRING(500),
      allowNull: true,
      field: 'action_url'
    },
    actionRequired: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'action_required'
    },
    metadata: {
      type: DataTypes.JSONB,
      allowNull: true
    },
    sentVia: {
      type: DataTypes.ARRAY(DataTypes.STRING),
      defaultValue: [],
      field: 'sent_via'
    },
    emailSent: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'email_sent'
    },
    smsSent: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'sms_sent'
    },
    pushSent: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'push_sent'
    },
    readAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'read_at'
    },
    expiresAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'expires_at'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    }
  },
  {
    sequelize,
    tableName: 'notifications',
    timestamps: false,
    underscored: true
  }
);

export { Notification };

