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 { AcademicYears } from './academic_years';
import type { AttendanceSessions } from './attendance_sessions';
import type { Campuses } from './campuses';
import type { ClassEnrollments } from './class_enrollments';
import type { ClassSubjects } from './class_subjects';
import type { Grades } from './grades';
import type { Organizations } from './organizations';
import type { Staff } from './staff';
import type { Users } from './users';

export class Classes extends Model<
  InferAttributes<Classes>,
  InferCreationAttributes<Classes>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  declare section: string | null;
  declare capacity: number | null;
  declare status: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare academic_yearId: CreationOptional<string | null>;
  declare campusId: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare gradeId: CreationOptional<string | null>;
  declare homeroom_teacherId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getClass_enrollments_class: HasManyGetAssociationsMixin<ClassEnrollments>;
  declare setClass_enrollments_class: HasManySetAssociationsMixin<ClassEnrollments, string>;
  declare getClass_subjects_class: HasManyGetAssociationsMixin<ClassSubjects>;
  declare setClass_subjects_class: HasManySetAssociationsMixin<ClassSubjects, string>;
  declare getAttendance_sessions_class: HasManyGetAssociationsMixin<AttendanceSessions>;
  declare setAttendance_sessions_class: HasManySetAssociationsMixin<AttendanceSessions, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getAcademic_year: BelongsToGetAssociationMixin<AcademicYears>;
  declare setAcademic_year: BelongsToSetAssociationMixin<AcademicYears, string>;
  declare getGrade: BelongsToGetAssociationMixin<Grades>;
  declare setGrade: BelongsToSetAssociationMixin<Grades, string>;
  declare getHomeroom_teacher: BelongsToGetAssociationMixin<Staff>;
  declare setHomeroom_teacher: 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.classes.hasMany(db.class_enrollments, {
      as: 'class_enrollments_class',
      foreignKey: {
          name: 'classId',
      },
      constraints: false,
    });


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




    db.classes.hasMany(db.attendance_sessions, {
      as: 'attendance_sessions_class',
      foreignKey: {
          name: 'classId',
      },
      constraints: false,
    });












//end loop



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

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

    db.classes.belongsTo(db.academic_years, {
      as: 'academic_year',
      foreignKey: {
        name: 'academic_yearId',
      },
      constraints: false,
    });

    db.classes.belongsTo(db.grades, {
      as: 'grade',
      foreignKey: {
        name: 'gradeId',
      },
      constraints: false,
    });

    db.classes.belongsTo(db.staff, {
      as: 'homeroom_teacher',
      foreignKey: {
        name: 'homeroom_teacherId',
      },
      constraints: false,
    });




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

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

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

name: {
        type: DataTypes.TEXT,
      
      

      },

section: {
        type: DataTypes.TEXT,
      
      

      },

capacity: {
        type: DataTypes.INTEGER,
      
      

      },

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 },
      academic_yearId: { type: DataTypes.UUID, allowNull: true },
      campusId: { type: DataTypes.UUID, allowNull: true },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      gradeId: { type: DataTypes.UUID, allowNull: true },
      homeroom_teacherId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'classes',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return Classes;
}
