/*
  Warnings:

  - You are about to drop the column `account_id` on the `sales_invoices` table. All the data in the column will be lost.
  - A unique constraint covering the columns `[invoice_number,pos_id]` on the table `sales_invoices` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `fiscal_id` to the `business_activities` table without a default value. This is not possible if the table is not empty.
  - Added the required column `partner_token` to the `business_activities` table without a default value. This is not possible if the table is not empty.
  - Added the required column `consumer_account_id` to the `sales_invoices` table without a default value. This is not possible if the table is not empty.
  - Added the required column `invoice_number` to the `sales_invoices` table without a default value. This is not possible if the table is not empty.
  - Made the column `invoice_date` on table `sales_invoices` required. This step will fail if there are existing NULL values in that column.

*/
-- DropForeignKey
ALTER TABLE `sales_invoices` DROP FOREIGN KEY `sales_invoices_account_id_fkey`;

-- DropIndex
DROP INDEX `sales_invoices_account_id_fkey` ON `sales_invoices`;

-- AlterTable
ALTER TABLE `business_activities` ADD COLUMN `fiscal_id` VARCHAR(191) NOT NULL DEFAULT '',
    ADD COLUMN `partner_token` VARCHAR(191) NOT NULL DEFAULT '';

-- AlterTable
ALTER TABLE `sales_invoice_items` ADD COLUMN `good_snapshot` JSON NULL;

-- AlterTable
ALTER TABLE `sales_invoice_payments` MODIFY `paid_at` TIMESTAMP(0) NOT NULL,
    MODIFY `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0);

-- AlterTable
ALTER TABLE `sales_invoices` DROP COLUMN `account_id`,
    ADD COLUMN `consumer_account_id` VARCHAR(191) NOT NULL,
    ADD COLUMN `invoice_number` INTEGER NOT NULL,
    MODIFY `invoice_date` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0);

-- CreateTable
CREATE TABLE `sale_invoice_fiscals` (
    `id` VARCHAR(191) NOT NULL,
    `retry_count` INTEGER NOT NULL DEFAULT 0,
    `last_attempt_at` TIMESTAMP(0) NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NOT NULL,
    `invoice_id` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `sale_invoice_fiscals_invoice_id_key`(`invoice_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `sale_invoice_fiscal_attempts` (
    `id` VARCHAR(191) NOT NULL,
    `attempt_no` INTEGER NOT NULL,
    `status` VARCHAR(50) NOT NULL,
    `tax_id` VARCHAR(191) NULL,
    `request_payload` JSON NULL,
    `response_payload` JSON NULL,
    `error_message` TEXT NULL,
    `sent_at` TIMESTAMP(0) NULL,
    `received_at` TIMESTAMP(0) NULL,
    `created_at` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `fiscal_id` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `sale_invoice_fiscal_attempts_tax_id_key`(`tax_id`),
    INDEX `sale_invoice_fiscal_attempts_status_idx`(`status`),
    INDEX `sale_invoice_fiscal_attempts_tax_id_idx`(`tax_id`),
    UNIQUE INDEX `sale_invoice_fiscal_attempts_fiscal_id_attempt_no_key`(`fiscal_id`, `attempt_no`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateIndex
CREATE UNIQUE INDEX `sales_invoices_invoice_number_pos_id_key` ON `sales_invoices`(`invoice_number`, `pos_id`);

-- AddForeignKey
ALTER TABLE `sales_invoices` ADD CONSTRAINT `sales_invoices_consumer_account_id_fkey` FOREIGN KEY (`consumer_account_id`) REFERENCES `consumer_accounts`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sale_invoice_fiscals` ADD CONSTRAINT `sale_invoice_fiscals_invoice_id_fkey` FOREIGN KEY (`invoice_id`) REFERENCES `sales_invoices`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `sale_invoice_fiscal_attempts` ADD CONSTRAINT `sale_invoice_fiscal_attempts_fiscal_id_fkey` FOREIGN KEY (`fiscal_id`) REFERENCES `sale_invoice_fiscals`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
