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

export class AcademicYears extends Model<
  InferAttributes<AcademicYears>,
  InferCreationAttributes<AcademicYears>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  declare start_date: Date | null;
  declare end_date: Date | null;
  declare current: CreationOptional<boolean>;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare organizationId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getClasses_academic_year: HasManyGetAssociationsMixin<Classes>;
  declare setClasses_academic_year: HasManySetAssociationsMixin<Classes, string>;
  declare getTimetables_academic_year: HasManyGetAssociationsMixin<Timetables>;
  declare setTimetables_academic_year: HasManySetAssociationsMixin<Timetables, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, 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.academic_years.hasMany(db.classes, {
      as: 'classes_academic_year',
      foreignKey: {
          name: 'academic_yearId',
      },
      constraints: false,
    });




    db.academic_years.hasMany(db.timetables, {
      as: 'timetables_academic_year',
      foreignKey: {
          name: 'academic_yearId',
      },
      constraints: false,
    });














//end loop



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




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

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

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

name: {
        type: DataTypes.TEXT,
      
      

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

end_date: {
        type: DataTypes.DATE,
      
      

      },

current: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  return AcademicYears;
}
