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 qr1: JSON;
  declare qr2: Blob;

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

    VerifactuRecordModel.belongsTo(VerifactuRecordModel, {
      as: "verifactu-record",
      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,
        defaultValue: null,
      },

      qr1: {
        type: new DataTypes.JSON(),
        allowNull: false,
        defaultValue: null,
      },

      qr2: {
        type: new DataTypes.BLOB(),
        allowNull: false,
        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;
};
