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

export class TimetablePeriods extends Model<
  InferAttributes<TimetablePeriods>,
  InferCreationAttributes<TimetablePeriods>
> {
  declare id: CreationOptional<string>;
  declare day_of_week: string | null;
  declare starts_at: Date | null;
  declare ends_at: Date | null;
  declare room: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare class_subjectId: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare timetableId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getTimetable: BelongsToGetAssociationMixin<Timetables>;
  declare setTimetable: BelongsToSetAssociationMixin<Timetables, string>;
  declare getClass_subject: BelongsToGetAssociationMixin<ClassSubjects>;
  declare setClass_subject: BelongsToSetAssociationMixin<ClassSubjects, 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.timetable_periods.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.timetable_periods.belongsTo(db.timetables, {
      as: 'timetable',
      foreignKey: {
        name: 'timetableId',
      },
      constraints: false,
    });

    db.timetable_periods.belongsTo(db.class_subjects, {
      as: 'class_subject',
      foreignKey: {
        name: 'class_subjectId',
      },
      constraints: false,
    });




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

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

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

day_of_week: {
        type: DataTypes.ENUM,
      
      

        values: [

"monday",


"tuesday",


"wednesday",


"thursday",


"friday",


"saturday",


"sunday"

        ],

      },

starts_at: {
        type: DataTypes.DATE,
      
      

      },

ends_at: {
        type: DataTypes.DATE,
      
      

      },

room: {
        type: DataTypes.TEXT,
      
      

      },

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

  return TimetablePeriods;
}
