ALTER TABLE `cash_session_operations`
    MODIFY `type` ENUM('OPENING', 'OPENING_EDIT', 'MANUAL_IN', 'MANUAL_OUT', 'DEPOSIT_REFUND', 'CLOSING', 'REOPEN') NOT NULL;

ALTER TABLE `orders`
    ADD COLUMN `deposit_total` INT NOT NULL DEFAULT 0;

CREATE TABLE `deposit_definitions` (
    `id` VARCHAR(191) NOT NULL,
    `shop_id` VARCHAR(191) NOT NULL,
    `label` VARCHAR(191) NOT NULL,
    `amount` INT NOT NULL,
    `active` BOOLEAN NOT NULL DEFAULT true,
    `display_order` INT NOT NULL DEFAULT 0,
    `code` VARCHAR(191) NULL,
    `description` TEXT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    PRIMARY KEY (`id`),
    UNIQUE INDEX `deposit_definitions_shop_id_label_key`(`shop_id`, `label`),
    INDEX `deposit_definitions_shop_id_active_idx`(`shop_id`, `active`),
    INDEX `deposit_definitions_shop_id_display_order_idx`(`shop_id`, `display_order`),
    CONSTRAINT `deposit_definitions_shop_id_fkey`
        FOREIGN KEY (`shop_id`) REFERENCES `shops`(`id`)
        ON DELETE CASCADE ON UPDATE CASCADE
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE TABLE `order_deposit_lines` (
    `id` VARCHAR(191) NOT NULL,
    `order_id` VARCHAR(191) NOT NULL,
    `deposit_definition_id` VARCHAR(191) NOT NULL,
    `label_snapshot` VARCHAR(191) NOT NULL,
    `unit_amount_snapshot` INT NOT NULL,
    `quantity` INT NOT NULL,
    `total_amount` INT NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`),
    INDEX `order_deposit_lines_order_id_idx`(`order_id`),
    INDEX `order_deposit_lines_deposit_definition_id_idx`(`deposit_definition_id`),
    CONSTRAINT `order_deposit_lines_order_id_fkey`
        FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`)
        ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `order_deposit_lines_deposit_definition_id_fkey`
        FOREIGN KEY (`deposit_definition_id`) REFERENCES `deposit_definitions`(`id`)
        ON DELETE RESTRICT ON UPDATE CASCADE
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE TABLE `cash_session_operation_deposit_lines` (
    `id` VARCHAR(191) NOT NULL,
    `cash_session_operation_id` VARCHAR(191) NOT NULL,
    `deposit_definition_id` VARCHAR(191) NOT NULL,
    `label_snapshot` VARCHAR(191) NOT NULL,
    `unit_amount_snapshot` INT NOT NULL,
    `quantity` INT NOT NULL,
    `total_amount` INT NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`),
    INDEX `csodl_cash_session_operation_id_idx`(`cash_session_operation_id`),
    INDEX `csodl_deposit_definition_id_idx`(`deposit_definition_id`),
    CONSTRAINT `csodl_operation_fkey`
        FOREIGN KEY (`cash_session_operation_id`) REFERENCES `cash_session_operations`(`id`)
        ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `csodl_definition_fkey`
        FOREIGN KEY (`deposit_definition_id`) REFERENCES `deposit_definitions`(`id`)
        ON DELETE RESTRICT ON UPDATE CASCADE
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

