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

export class AssessmentResults extends Model<
  InferAttributes<AssessmentResults>,
  InferCreationAttributes<AssessmentResults>
> {
  declare id: CreationOptional<string>;
  declare score: string | null;
  declare grade_letter: string | null;
  declare remarks: 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 assessmentId: CreationOptional<string | null>;
  declare studentId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getAssessment: BelongsToGetAssociationMixin<Assessments>;
  declare setAssessment: BelongsToSetAssociationMixin<Assessments, 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





























//end loop



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

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




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

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

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

score: {
        type: DataTypes.DECIMAL,
      
      

      },

grade_letter: {
        type: DataTypes.ENUM,
      
      

        values: [

"A",


"B",


"C",


"D",


"E",


"F",


"P",


"N"

        ],

      },

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

  return AssessmentResults;
}
