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

/**
 * Direct 1:1 message between a staff member and a guardian. A conversation is
 * the unordered pair {senderId, recipientId}; `studentId` records the student
 * the two were connected through. Read access is membership-based (sender or
 * recipient only), which keeps every conversation isolated.
 */
export class DirectMessages extends Model<
  InferAttributes<DirectMessages>,
  InferCreationAttributes<DirectMessages>
> {
  declare id: CreationOptional<string>;
  declare senderId: string;
  declare recipientId: string;
  declare studentId: CreationOptional<string | null>;
  declare body: string;
  declare readAt: CreationOptional<Date | null>;
  declare organizationId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;

  declare getSender: BelongsToGetAssociationMixin<Users>;
  declare getRecipient: BelongsToGetAssociationMixin<Users>;
  declare getStudent: BelongsToGetAssociationMixin<Users>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;

  static associate(db: Db): void {
    db.direct_messages.belongsTo(db.users, {
      as: 'sender',
      foreignKey: { name: 'senderId' },
      constraints: false,
    });
    db.direct_messages.belongsTo(db.users, {
      as: 'recipient',
      foreignKey: { name: 'recipientId' },
      constraints: false,
    });
    db.direct_messages.belongsTo(db.users, {
      as: 'student',
      foreignKey: { name: 'studentId' },
      constraints: false,
    });
    db.direct_messages.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: { name: 'organizationId' },
      constraints: false,
    });
  }
}

export default function (sequelize: Sequelize): typeof DirectMessages {
  DirectMessages.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      senderId: { type: DataTypes.UUID, allowNull: false },
      recipientId: { type: DataTypes.UUID, allowNull: false },
      studentId: { type: DataTypes.UUID, allowNull: true },
      body: { type: DataTypes.TEXT, allowNull: false },
      readAt: { type: DataTypes.DATE, allowNull: true },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
    },
    {
      sequelize,
      modelName: 'direct_messages',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return DirectMessages;
}
