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 { AttendanceRecords } from './attendance_records';
import type { Campuses } from './campuses';
import type { ClassSubjects } from './class_subjects';
import type { Classes } from './classes';
import type { Organizations } from './organizations';
import type { Staff } from './staff';
import type { Users } from './users';

export class AttendanceSessions extends Model<
  InferAttributes<AttendanceSessions>,
  InferCreationAttributes<AttendanceSessions>
> {
  declare id: CreationOptional<string>;
  declare session_date: Date | null;
  declare session_type: string | null;
  declare notes: 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 campusId: CreationOptional<string | null>;
  declare classId: CreationOptional<string | null>;
  declare class_subjectId: CreationOptional<string | null>;
  declare taken_byId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getAttendance_records_attendance_session: HasManyGetAssociationsMixin<AttendanceRecords>;
  declare setAttendance_records_attendance_session: HasManySetAssociationsMixin<AttendanceRecords, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getClass: BelongsToGetAssociationMixin<Classes>;
  declare setClass: BelongsToSetAssociationMixin<Classes, string>;
  declare getClass_subject: BelongsToGetAssociationMixin<ClassSubjects>;
  declare setClass_subject: BelongsToSetAssociationMixin<ClassSubjects, string>;
  declare getTaken_by: BelongsToGetAssociationMixin<Staff>;
  declare setTaken_by: 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.attendance_sessions.hasMany(db.attendance_records, {
      as: 'attendance_records_attendance_session',
      foreignKey: {
          name: 'attendance_sessionId',
      },
      constraints: false,
    });











//end loop



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

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

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

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

    db.attendance_sessions.belongsTo(db.staff, {
      as: 'taken_by',
      foreignKey: {
        name: 'taken_byId',
      },
      constraints: false,
    });




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

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

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

session_date: {
        type: DataTypes.DATE,
      
      

      },

session_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"homeroom",


"subject",


"exam",


"event"

        ],

      },

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

  return AttendanceSessions;
}
