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 {
  POLICY_DOCUMENT_CATEGORY_VALUES,
  type PolicyDocumentCategory,
} from '@/shared/constants/policy-documents';
import type { Organizations } from './organizations';
import type { Campuses } from './campuses';
import type { Users } from './users';

export class PolicyDocuments extends Model<
  InferAttributes<PolicyDocuments>,
  InferCreationAttributes<PolicyDocuments>
> {
  declare id: CreationOptional<string>;
  declare title: string;
  declare body: CreationOptional<string | null>;
  declare category: PolicyDocumentCategory;
  declare tag: CreationOptional<string | null>;
  declare author: CreationOptional<string | null>;
  /** Author-filled structured content (safety protocols): ordered steps. */
  declare steps: CreationOptional<string[] | null>;
  /** Author-filled structured content (safety protocols): autism considerations. */
  declare autism_considerations: CreationOptional<string[] | null>;
  declare version: CreationOptional<number>;
  declare active: CreationOptional<boolean>;
  declare importHash: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare campusId: 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 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.policy_documents.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: { name: 'organizationId' },
      constraints: false,
    });
    db.policy_documents.belongsTo(db.campuses, {
      as: 'campus',
      foreignKey: { name: 'campusId' },
      constraints: false,
    });
    db.policy_documents.belongsTo(db.users, { as: 'createdBy' });
    db.policy_documents.belongsTo(db.users, { as: 'updatedBy' });
  }
}

export default function (sequelize: Sequelize): typeof PolicyDocuments {
  PolicyDocuments.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      title: { type: DataTypes.TEXT, allowNull: false },
      body: { type: DataTypes.TEXT, allowNull: true },
      category: {
        type: DataTypes.ENUM(...POLICY_DOCUMENT_CATEGORY_VALUES),
        allowNull: false,
      },
      tag: { type: DataTypes.STRING(255), allowNull: true },
      author: { type: DataTypes.STRING(255), allowNull: true },
      steps: { type: DataTypes.JSONB, allowNull: true },
      autism_considerations: { type: DataTypes.JSONB, allowNull: true },
      version: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 1 },
      active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      campusId: { 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: 'policy_documents',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return PolicyDocuments;
}
