<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `sale_items` (
            `id`                BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `sale_id`           BIGINT UNSIGNED NOT NULL,
            `item_id`           BIGINT UNSIGNED NULL COMMENT 'NULL if package line',
            `package_id`        BIGINT UNSIGNED NULL COMMENT 'NULL if single item line',
            `item_name_ar`      VARCHAR(300) NOT NULL COMMENT 'Snapshot at time of sale',
            `quantity`          DECIMAL(15,3) NOT NULL,
            `unit_price`        DECIMAL(15,2) NOT NULL,
            `discount_amount`   DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `tax_amount`        DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `line_total`        DECIMAL(15,2) NOT NULL,
            `cost_price`        DECIMAL(15,2) NULL COMMENT 'Snapshot for profit calc',
            `batch_id`          BIGINT UNSIGNED NULL,
            `is_refunded`       TINYINT(1) NOT NULL DEFAULT 0,
            `refunded_quantity` DECIMAL(15,3) NOT NULL DEFAULT 0.000,
            INDEX `idx_sale_items_sale` (`sale_id`),
            INDEX `idx_sale_items_item` (`item_id`),
            INDEX `idx_sale_items_package` (`package_id`),
            INDEX `idx_sale_items_batch` (`batch_id`),
            CONSTRAINT `fk_sale_items_sale` FOREIGN KEY (`sale_id`) REFERENCES `sales`(`id`) ON DELETE CASCADE,
            CONSTRAINT `fk_sale_items_item` FOREIGN KEY (`item_id`) REFERENCES `inventory_items`(`id`) ON DELETE SET NULL,
            CONSTRAINT `fk_sale_items_package` FOREIGN KEY (`package_id`) REFERENCES `packages`(`id`) ON DELETE SET NULL,
            CONSTRAINT `fk_sale_items_batch` FOREIGN KEY (`batch_id`) REFERENCES `item_batches`(`id`) ON DELETE SET NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `sale_items`",
];
