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 { Assessments } from './assessments';
import type { AttendanceSessions } from './attendance_sessions';
import type { Classes } from './classes';
import type { Organizations } from './organizations';
import type { Staff } from './staff';
import type { Subjects } from './subjects';
import type { TimetablePeriods } from './timetable_periods';
import type { Users } from './users';

export class ClassSubjects extends Model<
  InferAttributes<ClassSubjects>,
  InferCreationAttributes<ClassSubjects>
> {
  declare id: CreationOptional<string>;
  declare status: 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 classId: CreationOptional<string | null>;
  declare subjectId: CreationOptional<string | null>;
  declare teacherId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getTimetable_periods_class_subject: HasManyGetAssociationsMixin<TimetablePeriods>;
  declare setTimetable_periods_class_subject: HasManySetAssociationsMixin<TimetablePeriods, string>;
  declare getAttendance_sessions_class_subject: HasManyGetAssociationsMixin<AttendanceSessions>;
  declare setAttendance_sessions_class_subject: HasManySetAssociationsMixin<AttendanceSessions, string>;
  declare getAssessments_class_subject: HasManyGetAssociationsMixin<Assessments>;
  declare setAssessments_class_subject: HasManySetAssociationsMixin<Assessments, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getClass: BelongsToGetAssociationMixin<Classes>;
  declare setClass: BelongsToSetAssociationMixin<Classes, string>;
  declare getSubject: BelongsToGetAssociationMixin<Subjects>;
  declare setSubject: BelongsToSetAssociationMixin<Subjects, string>;
  declare getTeacher: BelongsToGetAssociationMixin<Staff>;
  declare setTeacher: BelongsToSetAssociationMixin<Staff, 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.class_subjects.hasMany(db.timetable_periods, {
      as: 'timetable_periods_class_subject',
      foreignKey: {
          name: 'class_subjectId',
      },
      constraints: false,
    });


    db.class_subjects.hasMany(db.attendance_sessions, {
      as: 'attendance_sessions_class_subject',
      foreignKey: {
          name: 'class_subjectId',
      },
      constraints: false,
    });






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







//end loop



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

    db.class_subjects.belongsTo(db.classes, {
      as: 'class',
      foreignKey: {
        name: 'classId',
      },
      constraints: false,
    });

    db.class_subjects.belongsTo(db.subjects, {
      as: 'subject',
      foreignKey: {
        name: 'subjectId',
      },
      constraints: false,
    });

    db.class_subjects.belongsTo(db.staff, {
      as: 'teacher',
      foreignKey: {
        name: 'teacherId',
      },
      constraints: false,
    });




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

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

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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"archived"

        ],

      },

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

  return ClassSubjects;
}
