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 { AssessmentResults } from './assessment_results';
import type { Assessments } from './assessments';
import type { AttendanceRecords } from './attendance_records';
import type { AttendanceSessions } from './attendance_sessions';
import type { Campuses } from './campuses';
import type { ClassEnrollments } from './class_enrollments';
import type { ClassSubjects } from './class_subjects';
import type { Classes } from './classes';
import type { Grades } from './grades';
import type { MessageRecipients } from './message_recipients';
import type { Messages } from './messages';
import type { Staff } from './staff';
import type { Subjects } from './subjects';
import type { TimetablePeriods } from './timetable_periods';
import type { Timetables } from './timetables';
import type { Users } from './users';

export class Organizations extends Model<
  InferAttributes<Organizations>,
  InferCreationAttributes<Organizations>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getUsers_organizations: HasManyGetAssociationsMixin<Users>;
  declare setUsers_organizations: HasManySetAssociationsMixin<Users, string>;
  declare getCampuses_organization: HasManyGetAssociationsMixin<Campuses>;
  declare setCampuses_organization: HasManySetAssociationsMixin<Campuses, string>;
  declare getAcademic_years_organization: HasManyGetAssociationsMixin<AcademicYears>;
  declare setAcademic_years_organization: HasManySetAssociationsMixin<AcademicYears, string>;
  declare getGrades_organization: HasManyGetAssociationsMixin<Grades>;
  declare setGrades_organization: HasManySetAssociationsMixin<Grades, string>;
  declare getSubjects_organization: HasManyGetAssociationsMixin<Subjects>;
  declare setSubjects_organization: HasManySetAssociationsMixin<Subjects, string>;
  declare getStaff_organization: HasManyGetAssociationsMixin<Staff>;
  declare setStaff_organization: HasManySetAssociationsMixin<Staff, string>;
  declare getClasses_organization: HasManyGetAssociationsMixin<Classes>;
  declare setClasses_organization: HasManySetAssociationsMixin<Classes, string>;
  declare getClass_enrollments_organization: HasManyGetAssociationsMixin<ClassEnrollments>;
  declare setClass_enrollments_organization: HasManySetAssociationsMixin<ClassEnrollments, string>;
  declare getClass_subjects_organization: HasManyGetAssociationsMixin<ClassSubjects>;
  declare setClass_subjects_organization: HasManySetAssociationsMixin<ClassSubjects, string>;
  declare getTimetables_organization: HasManyGetAssociationsMixin<Timetables>;
  declare setTimetables_organization: HasManySetAssociationsMixin<Timetables, string>;
  declare getTimetable_periods_organization: HasManyGetAssociationsMixin<TimetablePeriods>;
  declare setTimetable_periods_organization: HasManySetAssociationsMixin<TimetablePeriods, string>;
  declare getAttendance_sessions_organization: HasManyGetAssociationsMixin<AttendanceSessions>;
  declare setAttendance_sessions_organization: HasManySetAssociationsMixin<AttendanceSessions, string>;
  declare getAttendance_records_organization: HasManyGetAssociationsMixin<AttendanceRecords>;
  declare setAttendance_records_organization: HasManySetAssociationsMixin<AttendanceRecords, string>;
  declare getAssessments_organization: HasManyGetAssociationsMixin<Assessments>;
  declare setAssessments_organization: HasManySetAssociationsMixin<Assessments, string>;
  declare getAssessment_results_organization: HasManyGetAssociationsMixin<AssessmentResults>;
  declare setAssessment_results_organization: HasManySetAssociationsMixin<AssessmentResults, string>;
  declare getMessages_organization: HasManyGetAssociationsMixin<Messages>;
  declare setMessages_organization: HasManySetAssociationsMixin<Messages, string>;
  declare getMessage_recipients_organization: HasManyGetAssociationsMixin<MessageRecipients>;
  declare setMessage_recipients_organization: HasManySetAssociationsMixin<MessageRecipients, 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.organizations.hasMany(db.users, {
      as: 'users_organizations',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });





    db.organizations.hasMany(db.campuses, {
      as: 'campuses_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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


    db.organizations.hasMany(db.grades, {
      as: 'grades_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.subjects, {
      as: 'subjects_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });




    db.organizations.hasMany(db.staff, {
      as: 'staff_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.classes, {
      as: 'classes_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.class_enrollments, {
      as: 'class_enrollments_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.class_subjects, {
      as: 'class_subjects_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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


    db.organizations.hasMany(db.timetable_periods, {
      as: 'timetable_periods_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.attendance_sessions, {
      as: 'attendance_sessions_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


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





    db.organizations.hasMany(db.assessments, {
      as: 'assessments_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.assessment_results, {
      as: 'assessment_results_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.messages, {
      as: 'messages_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });


    db.organizations.hasMany(db.message_recipients, {
      as: 'message_recipients_organization',
      foreignKey: {
          name: 'organizationId',
      },
      constraints: false,
    });




//end loop






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

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

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

name: {
        type: DataTypes.TEXT,
      
      

      },

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

  return Organizations;
}
