import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import {
  COMMUNICATION_EVENT_TYPE_VALUES,
  type CommunicationEventType,
} from '@/shared/constants/communications';
import type { RoleName } from '@/shared/constants/roles';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
} from 'sequelize';
import type { Campuses } from './campuses';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class CommunicationEvents extends Model<
  InferAttributes<CommunicationEvents>,
  InferCreationAttributes<CommunicationEvents>
> {
  declare id: CreationOptional<string>;
  declare title: string;
  declare event_date: string;
  declare event_type: CommunicationEventType;
  declare roles: CreationOptional<RoleName[]>;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare organizationId: CreationOptional<string>;
  declare campusId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getCreatedBy: BelongsToGetAssociationMixin<Users>;
  declare setCreatedBy: BelongsToSetAssociationMixin<Users, string>;
  declare getUpdatedBy: BelongsToGetAssociationMixin<Users>;
  declare setUpdatedBy: BelongsToSetAssociationMixin<Users, string>;

  static associate(db: Db): void {
    db.communication_events.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

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

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

export default function (sequelize: Sequelize): typeof CommunicationEvents {
  CommunicationEvents.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      title: {
        type: DataTypes.TEXT,
        allowNull: false,
      },
      event_date: {
        type: DataTypes.DATEONLY,
        allowNull: false,
      },
      event_type: {
        type: DataTypes.ENUM(...COMMUNICATION_EVENT_TYPE_VALUES),
        allowNull: false,
      },
      roles: {
        type: DataTypes.JSONB,
        allowNull: false,
        defaultValue: [],
      },
      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: false },
      campusId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: false },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'communication_events',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return CommunicationEvents;
}
