import { Sequelize, DataTypes, Model, Optional } from 'sequelize';
import { TrackexNotificationAttributes } from './trackex_notifications';
import { UserAttributes } from './User';

export interface NotificationHistoryAttributes {
  id: number;
  notification_id: number;
  target_to: number;
  user_id: number;
  is_seen: boolean;
  is_read: boolean;
  is_active: boolean;
  notification?: TrackexNotificationAttributes;
  user?: UserAttributes; 
  sender?: UserAttributes;
}

interface NotificationHistoryCreationAttributes
  extends Optional<NotificationHistoryAttributes, 'id'> {}

export class NotificationHistory
  extends Model<NotificationHistoryAttributes, NotificationHistoryCreationAttributes>
  implements NotificationHistoryAttributes {
  public id!: number;
  public notification_id!: number;
  public target_to!: number;
  public user_id!: number;
  public is_seen!: boolean;
  public is_read!: boolean;
  public is_active!: boolean;

  public notification?: TrackexNotificationAttributes;
  public user?: UserAttributes;
  public sender?: UserAttributes;

  public static associate(models: any) {
    NotificationHistory.belongsTo(models.TrackexNotification, {
      foreignKey: 'notification_id',
      as: 'notification',
    });

    NotificationHistory.belongsTo(models.User, {
      foreignKey: 'target_to',
      as: 'user',
    });

    NotificationHistory.belongsTo(models.User, {
      foreignKey: 'user_id',
      as: 'sender',
    });
  }

  public toJSON(): NotificationHistoryAttributes {
    return {
      ...super.toJSON(),
      id: this.id,
      notification_id: this.notification_id,
      target_to: this.target_to,
      user_id: this.user_id,
      is_seen: this.is_seen,
      is_read: this.is_read,
      is_active: this.is_active,
      notification: this.notification,
      user: this.user,
      sender: this.sender,
    };
  }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
  NotificationHistory.init(
    {
      id: {
        type: dataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      notification_id: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'trackex_notifications',
          key: 'id',
        },
      },
      target_to: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id',
        },
      },
      user_id: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'users',
          key: 'id',
        },
      },
      is_seen: {
        type: dataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      is_read: {
        type: dataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      is_active: {
        type: dataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true,
      },
    },
    {
      sequelize,
      modelName: 'NotificationHistory',
      tableName: 'notification_histories',
      timestamps: true,
    }
  );

  return NotificationHistory;
};
