import { DataTypes, InferAttributes, InferCreationAttributes, Model, Sequelize } from "sequelize";
/*import {
  CustomerInvoiceItemTaxCreationAttributes,
  CustomerInvoiceItemTaxModel,
} from "./customer-invoice-item-tax.model";
 */

export type VerifactuRecordCreationAttributes = InferCreationAttributes<VerifactuRecordModel>;

export class VerifactuRecordModel extends Model<
  InferAttributes<VerifactuRecordModel>,
  InferCreationAttributes<VerifactuRecordModel>
> {
  declare id: string;
  declare invoice_id: string;

  declare estado: string;
  declare url: string;
  declare qr: Blob;

  declare uuid: string;
  declare operacion: string;

  static associate(database: Sequelize) {
    const { CustomerInvoiceModel } = database.models;

    VerifactuRecordModel.belongsTo(CustomerInvoiceModel, {
      as: "verifactu_records",
      targetKey: "id",
      foreignKey: "invoice_id",
      onDelete: "CASCADE",
      onUpdate: "CASCADE",
    });
  }
}

export default (database: Sequelize) => {
  VerifactuRecordModel.init(
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
      },

      invoice_id: {
        type: DataTypes.UUID,
        allowNull: false,
      },

      estado: {
        type: new DataTypes.TEXT(),
        allowNull: true,
        defaultValue: null,
      },

      url: {
        type: new DataTypes.TEXT(),
        allowNull: false,
      },

      qr: {
        type: new DataTypes.BLOB(),
        allowNull: false,
      },

      uuid: {
        type: DataTypes.STRING,
        allowNull: false,
      },

      operacion: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null,
      },
    },
    {
      sequelize: database,
      tableName: "verifactu_records",

      underscored: true,

      indexes: [],

      whereMergeStrategy: "and", // <- cómo tratar el merge de un scope

      defaultScope: {
        order: [["position", "ASC"]],
      },

      scopes: {},
    }
  );

  return VerifactuRecordModel;
};
