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 { Organizations } from './organizations';
import type { PolicyDocuments } from './policy_documents';
import type { Users } from './users';

export class PolicyAcknowledgments extends Model<
  InferAttributes<PolicyAcknowledgments>,
  InferCreationAttributes<PolicyAcknowledgments>
> {
  declare id: CreationOptional<string>;
  declare policyDocumentId: string;
  declare version: number;
  declare userId: string;
  declare acknowledgedAt: Date;
  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 getPolicyDocument: BelongsToGetAssociationMixin<PolicyDocuments>;
  declare setPolicyDocument: BelongsToSetAssociationMixin<PolicyDocuments, string>;
  declare getUser: BelongsToGetAssociationMixin<Users>;
  declare setUser: BelongsToSetAssociationMixin<Users, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;

  static associate(db: Db): void {
    db.policy_acknowledgments.belongsTo(db.policy_documents, {
      as: 'policyDocument',
      foreignKey: { name: 'policyDocumentId' },
      constraints: false,
    });
    db.policy_acknowledgments.belongsTo(db.users, {
      as: 'user',
      foreignKey: { name: 'userId' },
      constraints: false,
    });
    db.policy_acknowledgments.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: { name: 'organizationId' },
      constraints: false,
    });
  }
}

export default function (sequelize: Sequelize): typeof PolicyAcknowledgments {
  PolicyAcknowledgments.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      policyDocumentId: { type: DataTypes.UUID, allowNull: false },
      version: { type: DataTypes.INTEGER, allowNull: false },
      userId: { type: DataTypes.UUID, allowNull: false },
      acknowledgedAt: { type: DataTypes.DATE, allowNull: false },
      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 },
    },
    {
      sequelize,
      modelName: 'policy_acknowledgments',
      timestamps: true,
      freezeTableName: true,
    },
  );

  return PolicyAcknowledgments;
}
