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 { Campuses } from './campuses';
import type { FeePlans } from './fee_plans';
import type { File } from './file';
import type { Organizations } from './organizations';
import type { Payments } from './payments';
import type { Students } from './students';
import type { Users } from './users';

export class Invoices extends Model<
  InferAttributes<Invoices>,
  InferCreationAttributes<Invoices>
> {
  declare id: CreationOptional<string>;
  declare invoice_number: string | null;
  declare issue_date: Date | null;
  declare due_date: Date | null;
  declare subtotal: string | null;
  declare discount_amount: string | null;
  declare tax_amount: string | null;
  declare total_amount: string | null;
  declare balance_due: string | null;
  declare status: string | null;
  declare notes: string | null;
  declare importHash: CreationOptional<string | null>;
  declare createdAt: CreationOptional<Date>;
  declare updatedAt: CreationOptional<Date>;
  declare deletedAt: CreationOptional<Date | null>;
  declare campusId: CreationOptional<string | null>;
  declare fee_planId: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare studentId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getPayments_invoice: HasManyGetAssociationsMixin<Payments>;
  declare setPayments_invoice: HasManySetAssociationsMixin<Payments, string>;
  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getCampus: BelongsToGetAssociationMixin<Campuses>;
  declare setCampus: BelongsToSetAssociationMixin<Campuses, string>;
  declare getStudent: BelongsToGetAssociationMixin<Students>;
  declare setStudent: BelongsToSetAssociationMixin<Students, string>;
  declare getFee_plan: BelongsToGetAssociationMixin<FeePlans>;
  declare setFee_plan: BelongsToSetAssociationMixin<FeePlans, string>;
  declare getAttachments: HasManyGetAssociationsMixin<File>;
  declare setAttachments: HasManySetAssociationsMixin<File, 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.invoices.hasMany(db.payments, {
      as: 'payments_invoice',
      foreignKey: {
          name: 'invoiceId',
      },
      constraints: false,
    });








//end loop



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

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

    db.invoices.belongsTo(db.students, {
      as: 'student',
      foreignKey: {
        name: 'studentId',
      },
      constraints: false,
    });

    db.invoices.belongsTo(db.fee_plans, {
      as: 'fee_plan',
      foreignKey: {
        name: 'fee_planId',
      },
      constraints: false,
    });



    db.invoices.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.invoices.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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

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

invoice_number: {
        type: DataTypes.TEXT,
      
      

      },

issue_date: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

subtotal: {
        type: DataTypes.DECIMAL,
      
      

      },

discount_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

tax_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

total_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

balance_due: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"issued",


"partially_paid",


"paid",


"overdue",


"void"

        ],

      },

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

  return Invoices;
}
