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 { Grades } from './grades';
import type { Invoices } from './invoices';
import type { Organizations } from './organizations';
import type { Users } from './users';

export class FeePlans extends Model<
  InferAttributes<FeePlans>,
  InferCreationAttributes<FeePlans>
> {
  declare id: CreationOptional<string>;
  declare name: string | null;
  declare billing_cycle: string | null;
  declare total_amount: string | null;
  declare active: CreationOptional<boolean>;
  declare notes: 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 organizationId: CreationOptional<string | null>;
  declare gradeId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getInvoices_fee_plan: HasManyGetAssociationsMixin<Invoices>;
  declare setInvoices_fee_plan: HasManySetAssociationsMixin<Invoices, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getAcademic_year: BelongsToGetAssociationMixin<AcademicYears>;
  declare setAcademic_year: BelongsToSetAssociationMixin<AcademicYears, string>;
  declare getGrade: BelongsToGetAssociationMixin<Grades>;
  declare setGrade: BelongsToSetAssociationMixin<Grades, 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.fee_plans.hasMany(db.invoices, {
      as: 'invoices_fee_plan',
      foreignKey: {
          name: 'fee_planId',
      },
      constraints: false,
    });









//end loop



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

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

    db.fee_plans.belongsTo(db.grades, {
      as: 'grade',
      foreignKey: {
        name: 'gradeId',
      },
      constraints: false,
    });




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

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

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

name: {
        type: DataTypes.TEXT,
      
      

      },

billing_cycle: {
        type: DataTypes.ENUM,
      
      

        values: [

"one_time",


"monthly",


"termly",


"annual"

        ],

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  return FeePlans;
}
