import {
  CreationOptional,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  Model,
} from "sequelize";

export type InvoiceItemCreationAttributes = InferCreationAttributes<InvoiceItemModel, {}> & {};

export class InvoiceItemModel extends Model<
  InferAttributes<InvoiceItemModel>,
  InvoiceItemCreationAttributes
> {
  static associate(connection: Sequelize) {
    /*const { Invoice_Model, InvoiceItem_Model } = connection.models;

    InvoiceItem_Model.belongsTo(Invoice_Model, {
      as: "invoice",
      foreignKey: "invoice_id",
      onDelete: "CASCADE",
    });*/
  }

  declare item_id: string;
  declare invoice_id: string;

  declare parent_id: CreationOptional<string>;
  declare position: number;
  declare item_type: string;

  declare description: CreationOptional<string>;

  declare quantity_amount: CreationOptional<number>;
  declare quantity_scale: CreationOptional<number>;

  declare unit_price_amount: CreationOptional<number>;
  declare unit_price_scale: CreationOptional<number>;

  declare subtotal_amount: CreationOptional<number>;
  declare subtotal_scale: CreationOptional<number>;

  declare discount_amount: CreationOptional<number>;
  declare discount_scale: CreationOptional<number>;

  declare total_amount: CreationOptional<number>;
  declare total_scale: CreationOptional<number>;

  //declare invoice?: NonAttribute<InvoiceModel>;
}

export default (sequelize: Sequelize) => {
  InvoiceItemModel.init(
    {
      item_id: {
        type: new DataTypes.UUID(),
        primaryKey: true,
      },
      invoice_id: {
        type: new DataTypes.UUID(),
        primaryKey: true,
      },
      parent_id: {
        type: new DataTypes.UUID(),
        allowNull: true, // Puede ser nulo para elementos de nivel superior
      },
      position: {
        type: new DataTypes.MEDIUMINT(),
        autoIncrement: false,
        allowNull: false,
      },
      item_type: {
        type: new DataTypes.STRING(),
        allowNull: false,
        defaultValue: "simple",
      },
      description: {
        type: new DataTypes.TEXT(),
        allowNull: true,
      },

      quantity_amount: {
        type: new DataTypes.BIGINT(),
        allowNull: true,
        defaultValue: null,
      },
      quantity_scale: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
        defaultValue: null,
      },

      unit_price_amount: {
        type: new DataTypes.BIGINT(),
        allowNull: true,
        defaultValue: null,
      },
      unit_price_scale: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
        defaultValue: null,
      },

      /*tax_slug: {
        type: new DataTypes.DECIMAL(3, 2),
        allowNull: true,
      },
      tax_rate: {
        type: new DataTypes.DECIMAL(3, 2),
        allowNull: true,
      },
      tax_equalization: {
        type: new DataTypes.DECIMAL(3, 2),
        allowNull: true,
      },*/

      subtotal_amount: {
        type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
        allowNull: true,
        defaultValue: null,
      },
      subtotal_scale: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
        defaultValue: null,
      },

      discount_amount: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
        defaultValue: null,
      },
      discount_scale: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
        defaultValue: null,
      },

      /*tax_amount: {
        type: new DataTypes.BIGINT(),
        allowNull: true,
      },*/
      total_amount: {
        type: new DataTypes.BIGINT(), // importante: evita problemas de precisión con valores grandes
        allowNull: true,
        defaultValue: null,
      },
      total_scale: {
        type: new DataTypes.SMALLINT(),
        allowNull: true,
        defaultValue: null,
      },
    },
    {
      sequelize,
      tableName: "invoice_items",

      defaultScope: {},

      scopes: {},
    }
  );

  return InvoiceItemModel;
};
