import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
} from 'sequelize';
import type { Messages } from './messages';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class MessageRecipients extends Model<
  InferAttributes<MessageRecipients>,
  InferCreationAttributes<MessageRecipients>
> {
  declare id: CreationOptional<string>;
  declare recipient_type: string | null;
  declare recipient_label: string | null;
  declare destination: string | null;
  declare delivery_status: string | null;
  declare delivered_at: Date | null;
  declare read_at: Date | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare organizationId: CreationOptional<string | null>;
  declare messageId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getMessage: BelongsToGetAssociationMixin<Messages>;
  declare setMessage: BelongsToSetAssociationMixin<Messages, string>;
  declare getCreatedBy: BelongsToGetAssociationMixin<Users>;
  declare setCreatedBy: BelongsToSetAssociationMixin<Users, string>;
  declare getUpdatedBy: BelongsToGetAssociationMixin<Users>;
  declare setUpdatedBy: BelongsToSetAssociationMixin<Users, string>;

  static associate(db: Db): void {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity





























//end loop



    db.message_recipients.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.message_recipients.belongsTo(db.messages, {
      as: 'message',
      foreignKey: {
        name: 'messageId',
      },
      constraints: false,
    });




    db.message_recipients.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.message_recipients.belongsTo(db.users, {
      as: 'updatedBy',
    });
  }
}

export default function (sequelize: Sequelize): typeof MessageRecipients {
  MessageRecipients.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

recipient_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"user",


"student",


"guardian"

        ],

      },

recipient_label: {
        type: DataTypes.TEXT,
      
      

      },

destination: {
        type: DataTypes.TEXT,
      
      

      },

delivery_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"sent",


"delivered",


"failed",


"read"

        ],

      },

delivered_at: {
        type: DataTypes.DATE,
      
      

      },

read_at: {
        type: DataTypes.DATE,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      messageId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'message_recipients',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return MessageRecipients;
}
