<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `stock_movements` (
            `id`               BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `warehouse_id`     BIGINT UNSIGNED NOT NULL,
            `item_id`          BIGINT UNSIGNED NOT NULL,
            `movement_type`    VARCHAR(30) NOT NULL COMMENT 'purchase_in, transfer_in, return_in, adjustment_in, opening_balance, sale_out, transfer_out, damage_out, expired_out, consumed_out, adjustment_out',
            `direction`        ENUM('in','out') NOT NULL,
            `quantity`         DECIMAL(15,3) NOT NULL,
            `unit_cost`        DECIMAL(15,2) NULL,
            `total_cost`       DECIMAL(15,2) NULL,
            `batch_id`         BIGINT UNSIGNED NULL,
            `reference_type`   VARCHAR(50) NULL COMMENT 'purchase_orders, sales, stock_transfers, stock_audits',
            `reference_id`     BIGINT UNSIGNED NULL,
            `transfer_id`      BIGINT UNSIGNED NULL,
            `notes`            TEXT NULL,
            `movement_date`    DATE NOT NULL,
            `branch_id`        BIGINT UNSIGNED NULL,
            `created_at`       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `created_by`       BIGINT UNSIGNED NULL,
            INDEX `idx_stock_movements_warehouse` (`warehouse_id`),
            INDEX `idx_stock_movements_item` (`item_id`),
            INDEX `idx_stock_movements_type` (`movement_type`),
            INDEX `idx_stock_movements_direction` (`direction`),
            INDEX `idx_stock_movements_date` (`movement_date`),
            INDEX `idx_stock_movements_batch` (`batch_id`),
            INDEX `idx_stock_movements_reference` (`reference_type`, `reference_id`),
            INDEX `idx_stock_movements_transfer` (`transfer_id`),
            INDEX `idx_stock_movements_item_date` (`item_id`, `movement_date`),
            INDEX `idx_stock_movements_warehouse_date` (`warehouse_id`, `movement_date`),
            CONSTRAINT `fk_stock_movements_warehouse` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses` (`id`) ON DELETE RESTRICT,
            CONSTRAINT `fk_stock_movements_item` FOREIGN KEY (`item_id`) REFERENCES `inventory_items` (`id`) ON DELETE RESTRICT,
            CONSTRAINT `chk_stock_movements_quantity` CHECK (`quantity` > 0)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `stock_movements`",
];
