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

export interface FollowUpAttributes {
  id: number;
  title: string;
  with_whom: string;
  assigned_to?: number;
  followup_date: Date;
  followup_notes?: string | null;
  status?: string;
  is_active?: boolean;
  reschedule_count?: number;
  created_by: number;
  followup_outcome?: string | null;
  reschedule_reason?: string | null;
}

interface FollowUpCreationAttributes extends Optional<FollowUpAttributes, 'id'> {}

export class FollowUp
  extends Model<FollowUpAttributes, FollowUpCreationAttributes>
  implements FollowUpAttributes
{
  public id!: number;
  public title!: string;
  public with_whom!: string;
  public assigned_to?: number;
  public followup_date!: Date;
  public followup_notes?: string | null;
  public status?: string;
  public is_active?: boolean;
  public reschedule_count?: number;
  public created_by!: number;
  public followup_outcome?: string | null;
  public reschedule_reason?: string | null;

  public assignee?: UserAttributes;
  public creator?: UserAttributes;

  public static associate(models: any): void {
    FollowUp.belongsTo(models.User, {
      foreignKey: 'assigned_to',
      as: 'assignee',
    });
    FollowUp.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator',
    });
    FollowUp.hasMany(models.FollowUpLog, {
      foreignKey: 'follow_up_id',
      as: 'followUpLogs',
    });
  }

  public toJSON(): FollowUpAttributes & {
    assignee?: UserAttributes;
    creator?: UserAttributes;
  } {
    return {
      ...super.toJSON(),
      id: this.id,
      title: this.title,
      with_whom: this.with_whom,
      assigned_to: this.assigned_to,
      followup_date: this.followup_date,
      followup_notes: this.followup_notes,
      status: this.status,
      is_active: this.is_active,
      reschedule_count: this.reschedule_count,
      created_by: this.created_by,
      followup_outcome: this.followup_outcome,
      reschedule_reason: this.reschedule_reason,
      assignee: this.assignee,
      creator: this.creator,
    };
  }
}

export default (sequelize: Sequelize, dataTypes: typeof DataTypes) => {
  FollowUp.init(
    {
      id: {
        type: dataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      title: {
        type: dataTypes.TEXT,
        allowNull: false,
      },
      with_whom: {
        type: dataTypes.TEXT,
        allowNull: false,
      },
      assigned_to: {
        type: dataTypes.INTEGER,
        allowNull: true,
        references: {
          model: 'Users',
          key: 'id',
        },
      },
      followup_date: {
        type: dataTypes.DATE,
        allowNull: false,
      },
      followup_notes: {
        type: dataTypes.TEXT,
        allowNull: true,
      },
      status: {
        type: dataTypes.STRING,
        allowNull: true,
      },
      reschedule_count: {
        type: dataTypes.INTEGER,
        allowNull: false
      },
      is_active: {
        type: dataTypes.BOOLEAN,
        defaultValue: true,
      },
      created_by: {
        type: dataTypes.INTEGER,
        allowNull: false,
        references: {
          model: 'Users',
          key: 'id',
        },
      },
      followup_outcome: {
        type: dataTypes.TEXT,
        allowNull: true,
      },
      reschedule_reason: {
        type: dataTypes.TEXT,
        allowNull: true,
      },
    },
    {
      sequelize,
      modelName: 'FollowUp',
      tableName: 'follow_ups',
      timestamps: true,
    }
  );

  return FollowUp;
};
