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

export class PersonalityQuizResults extends Model<
  InferAttributes<PersonalityQuizResults>,
  InferCreationAttributes<PersonalityQuizResults>
> {
  declare id: CreationOptional<string>;
  declare quiz_kind: string;
  declare quiz_id: string;
  declare quiz_title: string;
  declare week_of: CreationOptional<string | null>;
  declare personality_type: CreationOptional<string | null>;
  declare quiz_answers: unknown;
  declare score: CreationOptional<number | null>;
  declare total_questions: number;
  declare result_label: CreationOptional<string | null>;
  declare result_payload: unknown | null;
  declare user_name: CreationOptional<string | null>;
  declare user_role: CreationOptional<string | null>;
  declare completed_at: Date;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare organizationId: CreationOptional<string | null>;
  declare campusId: CreationOptional<string | null>;
  declare userId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getUser: BelongsToGetAssociationMixin<Users>;
  declare setUser: BelongsToSetAssociationMixin<Users, 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.personality_quiz_results.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

    db.personality_quiz_results.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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

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

export default function (sequelize: Sequelize): typeof PersonalityQuizResults {
  PersonalityQuizResults.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      personality_type: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      quiz_kind: {
        type: DataTypes.TEXT,
        allowNull: false,
        defaultValue: 'personality_type',
      },
      quiz_id: {
        type: DataTypes.TEXT,
        allowNull: false,
        defaultValue: 'personality-type',
      },
      quiz_title: {
        type: DataTypes.TEXT,
        allowNull: false,
        defaultValue: 'Personality Type Quiz',
      },
      week_of: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      quiz_answers: {
        type: DataTypes.JSONB,
        allowNull: false,
      },
      score: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      total_questions: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      result_label: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      result_payload: {
        type: DataTypes.JSONB,
        allowNull: true,
      },
      user_name: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      user_role: {
        type: DataTypes.TEXT,
        allowNull: true,
      },
      completed_at: {
        type: DataTypes.DATE,
        allowNull: false,
      },
      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: true },
      campusId: { type: DataTypes.UUID, allowNull: true },
      userId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'personality_quiz_results',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return PersonalityQuizResults;
}
