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

export interface FollowUpLogAttributes {
  id: number;
  follow_up_id: number;
  current_follow_up_date?: Date;
  new_follow_up?: Date;
  status?: string;
  reason?: string;
  user_id: number;
  transaction_type?: string,
  date_time?: Date,
  is_active?: boolean;
  createdAt?: Date;
  updatedAt?: Date;
}

interface FollowUpLogCreationAttributes extends Optional<FollowUpLogAttributes, 'id'> {}

export class FollowUpLog
  extends Model<FollowUpLogAttributes, FollowUpLogCreationAttributes>
  implements FollowUpLogAttributes
{
  public id!: number;
  public follow_up_id!: number;
  public current_follow_up_date?: Date;
  public new_follow_up?: Date;
  public status?: string;
  public reason?: string;
  public user_id!: number;
  public transaction_type?: string;
  public date_time?: Date;
  public is_active!: boolean;
  public createdAt!: Date;
  public updatedAt!: Date;

  public static associate(models: any): void {
    FollowUpLog.belongsTo(models.FollowUp, {
      foreignKey: 'follow_up_id',
      as: 'followUp',
    });
    FollowUpLog.belongsTo(models.User, {
      foreignKey: 'user_id',
      as: 'user',
    });
  }

  public toJSON(): FollowUpLogAttributes {
    return {
      ...this.get(),
    };
  }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
  FollowUpLog.init(
    {
      id: {
        type: dataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      follow_up_id: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'follow_ups',
          key: 'id',
        },
      },
      current_follow_up_date: {
        type: dataTypes.DATE,
        allowNull: true,
      },
      new_follow_up: {
        type: dataTypes.DATE,
        allowNull: true,
      },
      status: {
        type: dataTypes.STRING,
        allowNull: true,
      },
      reason: {
        type: dataTypes.TEXT,
        allowNull: true,
      },
      user_id: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'Users',
          key: 'id',
        },
      },
      transaction_type: {
        type: dataTypes.STRING,
        allowNull: true
      },
      date_time: {
        type: dataTypes.DATE,
        allowNull: true
      },
      is_active: {
        type: dataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true,
      },
      createdAt: {
        type: dataTypes.DATE,
        allowNull: false,
        defaultValue: dataTypes.NOW,
      },
      updatedAt: {
        type: dataTypes.DATE,
        allowNull: false,
        defaultValue: dataTypes.NOW,
      },
    },
    {
      sequelize,
      modelName: 'FollowUpLog',
      tableName: 'follow_up_logs',
      timestamps: true,
    }
  );

  return FollowUpLog;
};
