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 { Classes } from './classes';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class ClassEnrollments extends Model<
  InferAttributes<ClassEnrollments>,
  InferCreationAttributes<ClassEnrollments>
> {
  declare id: CreationOptional<string>;
  declare enrolled_on: Date | null;
  declare ended_on: 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 organizationId: CreationOptional<string | null>;
  declare classId: 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 getClass: BelongsToGetAssociationMixin<Classes>;
  declare setClass: BelongsToSetAssociationMixin<Classes, 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.class_enrollments.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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




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

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

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

enrolled_on: {
        type: DataTypes.DATE,
      
      

      },

ended_on: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"dropped",


"completed"

        ],

      },

      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 },
      classId: { 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: 'class_enrollments',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return ClassEnrollments;
}
