import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type NonAttribute,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
  HasManyGetAssociationsMixin,
  HasManySetAssociationsMixin,
} from 'sequelize';
import type { AttendanceSessions } from './attendance_sessions';
import type { Campuses } from './campuses';
import type { ClassSubjects } from './class_subjects';
import type { Classes } from './classes';
import type { File } from './file';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class Staff extends Model<
  InferAttributes<Staff>,
  InferCreationAttributes<Staff>
> {
  declare id: CreationOptional<string>;
  declare employee_number: string | null;
  declare job_title: string | null;
  declare staff_type: string | null;
  declare hire_date: Date | null;
  declare status: 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 userId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getClasses_homeroom_teacher: HasManyGetAssociationsMixin<Classes>;
  declare setClasses_homeroom_teacher: HasManySetAssociationsMixin<Classes, string>;
  declare getClass_subjects_teacher: HasManyGetAssociationsMixin<ClassSubjects>;
  declare setClass_subjects_teacher: HasManySetAssociationsMixin<ClassSubjects, string>;
  declare getAttendance_sessions_taken_by: HasManyGetAssociationsMixin<AttendanceSessions>;
  declare setAttendance_sessions_taken_by: HasManySetAssociationsMixin<AttendanceSessions, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  // Eager-loaded association (populated by `include`).
  declare campus?: NonAttribute<Campuses>;
  declare getUser: BelongsToGetAssociationMixin<Users>;
  declare setUser: BelongsToSetAssociationMixin<Users, 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.staff.hasMany(db.classes, {
      as: 'classes_homeroom_teacher',
      foreignKey: {
          name: 'homeroom_teacherId',
      },
      constraints: false,
    });



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




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












//end loop



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

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

    db.staff.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });



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


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

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

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

employee_number: {
        type: DataTypes.TEXT,
      
      

      },

job_title: {
        type: DataTypes.TEXT,
      
      

      },

staff_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"teacher",


"admin",


"support"

        ],

      },

hire_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"on_leave",


"inactive"

        ],

      },

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

  return Staff;
}
