import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
  HasManyGetAssociationsMixin,
  HasManySetAssociationsMixin,
} from 'sequelize';
import type { AssessmentResults } from './assessment_results';
import type { ClassSubjects } from './class_subjects';
import type { File } from './file';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class Assessments extends Model<
  InferAttributes<Assessments>,
  InferCreationAttributes<Assessments>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  declare assessment_type: string | null;
  declare assigned_at: Date | null;
  declare due_at: Date | null;
  declare max_score: string | null;
  declare status: string | null;
  declare instructions: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare organizationId: CreationOptional<string | null>;
  declare class_subjectId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getAssessment_results_assessment: HasManyGetAssociationsMixin<AssessmentResults>;
  declare setAssessment_results_assessment: HasManySetAssociationsMixin<AssessmentResults, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getClass_subject: BelongsToGetAssociationMixin<ClassSubjects>;
  declare setClass_subject: BelongsToSetAssociationMixin<ClassSubjects, string>;
  declare getAttachments: HasManyGetAssociationsMixin<File>;
  declare setAttachments: HasManySetAssociationsMixin<File, string>;
  declare getCreatedBy: BelongsToGetAssociationMixin<Users>;
  declare setCreatedBy: BelongsToSetAssociationMixin<Users, string>;
  declare getUpdatedBy: BelongsToGetAssociationMixin<Users>;
  declare setUpdatedBy: BelongsToSetAssociationMixin<Users, string>;

  static associate(db: Db): void {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
























    db.assessments.hasMany(db.assessment_results, {
      as: 'assessment_results_assessment',
      foreignKey: {
          name: 'assessmentId',
      },
      constraints: false,
    });






//end loop



    db.assessments.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.assessments.belongsTo(db.class_subjects, {
      as: 'class_subject',
      foreignKey: {
        name: 'class_subjectId',
      },
      constraints: false,
    });



    db.assessments.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.assessments.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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

export default function (sequelize: Sequelize): typeof Assessments {
  Assessments.init(
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

assessment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"quiz",


"homework",


"project",


"midterm",


"final",


"other"

        ],

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

max_score: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"closed"

        ],

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

      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 },
      class_subjectId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'assessments',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return Assessments;
}
