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 { AttendanceRecords } from './attendance_records';
import type { Campuses } from './campuses';
import type { ClassEnrollments } from './class_enrollments';
import type { File } from './file';
import type { Guardians } from './guardians';
import type { Invoices } from './invoices';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class Students extends Model<
  InferAttributes<Students>,
  InferCreationAttributes<Students>
> {
  declare id: CreationOptional<string>;
  declare student_number: string | null;
  declare first_name: string | null;
  declare last_name: string | null;
  declare gender: string | null;
  declare date_of_birth: Date | null;
  declare enrollment_date: Date | null;
  declare status: string | null;
  declare email: string | null;
  declare phone: string | null;
  declare address: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare campusId: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getGuardians_student: HasManyGetAssociationsMixin<Guardians>;
  declare setGuardians_student: HasManySetAssociationsMixin<Guardians, string>;
  declare getClass_enrollments_student: HasManyGetAssociationsMixin<ClassEnrollments>;
  declare setClass_enrollments_student: HasManySetAssociationsMixin<ClassEnrollments, string>;
  declare getAttendance_records_student: HasManyGetAssociationsMixin<AttendanceRecords>;
  declare setAttendance_records_student: HasManySetAssociationsMixin<AttendanceRecords, string>;
  declare getInvoices_student: HasManyGetAssociationsMixin<Invoices>;
  declare setInvoices_student: HasManySetAssociationsMixin<Invoices, string>;
  declare getAssessment_results_student: HasManyGetAssociationsMixin<AssessmentResults>;
  declare setAssessment_results_student: HasManySetAssociationsMixin<AssessmentResults, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getPhoto: HasManyGetAssociationsMixin<File>;
  declare setPhoto: 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.students.hasMany(db.guardians, {
      as: 'guardians_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });




    db.students.hasMany(db.class_enrollments, {
      as: 'class_enrollments_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });






    db.students.hasMany(db.attendance_records, {
      as: 'attendance_records_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });



    db.students.hasMany(db.invoices, {
      as: 'invoices_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });




    db.students.hasMany(db.assessment_results, {
      as: 'assessment_results_student',
      foreignKey: {
          name: 'studentId',
      },
      constraints: false,
    });






//end loop



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

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



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


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

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

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

student_number: {
        type: DataTypes.TEXT,
      
      

      },

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"male",


"female",


"other",


"prefer_not_to_say"

        ],

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

enrollment_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"prospect",


"enrolled",


"inactive",


"graduated",


"transferred"

        ],

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
      campusId: { type: DataTypes.UUID, allowNull: true },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'students',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return Students;
}
