import {
  DataTypes,
  Model,
  type CreationOptional,
  type InferAttributes,
  type InferCreationAttributes,
  type Sequelize,
} from 'sequelize';
import type { Db } from '@/db/types';
import type {
  BelongsToGetAssociationMixin,
  BelongsToSetAssociationMixin,
} from 'sequelize';
import type { AttendanceSessions } from './attendance_sessions';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class AttendanceRecords extends Model<
  InferAttributes<AttendanceRecords>,
  InferCreationAttributes<AttendanceRecords>
> {
  declare id: CreationOptional<string>;
  declare status: string | null;
  declare minutes_late: number | null;
  declare remarks: 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 attendance_sessionId: CreationOptional<string | null>;
  declare studentId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getAttendance_session: BelongsToGetAssociationMixin<AttendanceSessions>;
  declare setAttendance_session: BelongsToSetAssociationMixin<AttendanceSessions, 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





























//end loop



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

    db.attendance_records.belongsTo(db.attendance_sessions, {
      as: 'attendance_session',
      foreignKey: {
        name: 'attendance_sessionId',
      },
      constraints: false,
    });




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

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

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

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"present",


"absent",


"late",


"excused"

        ],

      },

minutes_late: {
        type: DataTypes.INTEGER,
      
      

      },

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

  return AttendanceRecords;
}
