import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
  HasManyGetAssociationsMixin,
  HasManySetAssociationsMixin,
} from 'sequelize';
import type { Campuses } from './campuses';
import type { File } from './file';
import type { MessageRecipients } from './message_recipients';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class Messages extends Model<
  InferAttributes<Messages>,
  InferCreationAttributes<Messages>
> {
  declare id: CreationOptional<string>;
  declare subject: string | null;
  declare body: string | null;
  declare channel: string | null;
  declare audience: string | null;
  declare sent_at: Date | null;
  declare status: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare campusId: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare sent_byId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getMessage_recipients_message: HasManyGetAssociationsMixin<MessageRecipients>;
  declare setMessage_recipients_message: HasManySetAssociationsMixin<MessageRecipients, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getSent_by: BelongsToGetAssociationMixin<Users>;
  declare setSent_by: BelongsToSetAssociationMixin<Users, string>;
  declare getAttachments: HasManyGetAssociationsMixin<File>;
  declare setAttachments: HasManySetAssociationsMixin<File, 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


























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




//end loop



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

    db.messages.belongsTo(db.campuses, {
      as: 'campus',
      foreignKey: {
        name: 'campusId',
      },
      constraints: false,
    });

    db.messages.belongsTo(db.users, {
      as: 'sent_by',
      foreignKey: {
        name: 'sent_byId',
      },
      constraints: false,
    });



    db.messages.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.messages.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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

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

subject: {
        type: DataTypes.TEXT,
      
      

      },

body: {
        type: DataTypes.TEXT,
      
      

      },

channel: {
        type: DataTypes.ENUM,
      
      

        values: [

"in_app",


"email",


"sms"

        ],

      },

audience: {
        type: DataTypes.ENUM,
      
      

        values: [

"all_org",


"campus",


"class",


"staff",


"students",


"guardians",


"custom"

        ],

      },

sent_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"scheduled",


"sent",


"failed"

        ],

      },

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

  return Messages;
}
