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 { AcademicYears } from './academic_years';
import type { Campuses } from './campuses';
import type { Organizations } from './organizations';
import type { TimetablePeriods } from './timetable_periods';
import type { Users } from './users';

export class Timetables extends Model<
  InferAttributes<Timetables>,
  InferCreationAttributes<Timetables>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  declare effective_from: Date | null;
  declare effective_to: 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 academic_yearId: CreationOptional<string | null>;
  declare campusId: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getTimetable_periods_timetable: HasManyGetAssociationsMixin<TimetablePeriods>;
  declare setTimetable_periods_timetable: HasManySetAssociationsMixin<TimetablePeriods, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getAcademic_year: BelongsToGetAssociationMixin<AcademicYears>;
  declare setAcademic_year: BelongsToSetAssociationMixin<AcademicYears, 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.timetables.hasMany(db.timetable_periods, {
      as: 'timetable_periods_timetable',
      foreignKey: {
          name: 'timetableId',
      },
      constraints: false,
    });













//end loop



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

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

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




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

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

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

name: {
        type: DataTypes.TEXT,
      
      

      },

effective_from: {
        type: DataTypes.DATE,
      
      

      },

effective_to: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"archived"

        ],

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
      createdAt: { type: DataTypes.DATE },
      updatedAt: { type: DataTypes.DATE },
      deletedAt: { type: DataTypes.DATE },
      academic_yearId: { type: DataTypes.UUID, allowNull: true },
      campusId: { type: DataTypes.UUID, allowNull: true },
      organizationId: { type: DataTypes.UUID, allowNull: true },
      createdById: { type: DataTypes.UUID, allowNull: true },
      updatedById: { type: DataTypes.UUID, allowNull: true },
    },
    {
      sequelize,
      modelName: 'timetables',
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  return Timetables;
}
