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 { File } from './file';
import type { Invoices } from './invoices';
import type { Organizations } from './organizations';
import type { Staff } from './staff';
import type { Users } from './users';

export class Payments extends Model<
  InferAttributes<Payments>,
  InferCreationAttributes<Payments>
> {
  declare id: CreationOptional<string>;
  declare receipt_number: string | null;
  declare paid_at: Date | null;
  declare amount: string | null;
  declare method: string | null;
  declare reference_code: 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 invoiceId: CreationOptional<string | null>;
  declare organizationId: CreationOptional<string | null>;
  declare received_byId: CreationOptional<string | null>;
  declare createdById: CreationOptional<string | null>;
  declare updatedById: CreationOptional<string | null>;


  declare getOrganization: BelongsToGetAssociationMixin<Organizations>;
  declare setOrganization: BelongsToSetAssociationMixin<Organizations, string>;
  declare getInvoice: BelongsToGetAssociationMixin<Invoices>;
  declare setInvoice: BelongsToSetAssociationMixin<Invoices, string>;
  declare getReceived_by: BelongsToGetAssociationMixin<Staff>;
  declare setReceived_by: BelongsToSetAssociationMixin<Staff, string>;
  declare getProof: HasManyGetAssociationsMixin<File>;
  declare setProof: 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





























//end loop



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

    db.payments.belongsTo(db.invoices, {
      as: 'invoice',
      foreignKey: {
        name: 'invoiceId',
      },
      constraints: false,
    });

    db.payments.belongsTo(db.staff, {
      as: 'received_by',
      foreignKey: {
        name: 'received_byId',
      },
      constraints: false,
    });



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


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

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

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

receipt_number: {
        type: DataTypes.TEXT,
      
      

      },

paid_at: {
        type: DataTypes.DATE,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

method: {
        type: DataTypes.ENUM,
      
      

        values: [

"cash",


"bank_transfer",


"card",


"mobile_money",


"cheque",


"other"

        ],

      },

reference_code: {
        type: DataTypes.TEXT,
      
      

      },

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

  return Payments;
}
