<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `sales` (
            `id`              BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `invoice_number`  VARCHAR(50) NOT NULL,
            `warehouse_id`    BIGINT UNSIGNED NOT NULL,
            `customer_type`   VARCHAR(20) NOT NULL COMMENT 'member, player, guest',
            `member_id`       BIGINT UNSIGNED NULL,
            `player_id`       BIGINT UNSIGNED NULL,
            `guest_name`      VARCHAR(200) NULL,
            `guest_phone`     VARCHAR(30) NULL,
            `subtotal`        DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `discount_amount` DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `discount_reason` VARCHAR(200) NULL,
            `tax_amount`      DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `total_amount`    DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `amount_paid`     DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `amount_refunded` DECIMAL(15,2) NOT NULL DEFAULT 0.00,
            `payment_id`      BIGINT UNSIGNED NULL,
            `payment_method`  VARCHAR(30) NULL,
            `status`          VARCHAR(30) NOT NULL DEFAULT 'draft' COMMENT 'draft, completed, partially_refunded, fully_refunded, voided',
            `sold_by`         BIGINT UNSIGNED NULL,
            `sale_date`       DATE NOT NULL,
            `notes`           TEXT NULL,
            `branch_id`       BIGINT UNSIGNED NULL,
            `is_archived`     TINYINT(1) NOT NULL DEFAULT 0,
            `archived_at`     TIMESTAMP NULL DEFAULT NULL,
            `archived_by`     BIGINT UNSIGNED NULL,
            `created_at`      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at`      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            `created_by`      BIGINT UNSIGNED NULL,
            `updated_by`      BIGINT UNSIGNED NULL,
            UNIQUE KEY `uq_sales_invoice_number` (`invoice_number`),
            INDEX `idx_sales_warehouse` (`warehouse_id`),
            INDEX `idx_sales_customer_type` (`customer_type`),
            INDEX `idx_sales_member` (`member_id`),
            INDEX `idx_sales_player` (`player_id`),
            INDEX `idx_sales_status` (`status`),
            INDEX `idx_sales_date` (`sale_date`),
            INDEX `idx_sales_payment` (`payment_id`),
            INDEX `idx_sales_sold_by` (`sold_by`),
            CONSTRAINT `fk_sales_warehouse` FOREIGN KEY (`warehouse_id`) REFERENCES `warehouses`(`id`) ON DELETE RESTRICT,
            CONSTRAINT `fk_sales_member` FOREIGN KEY (`member_id`) REFERENCES `members`(`id`) ON DELETE SET NULL,
            CONSTRAINT `fk_sales_player` FOREIGN KEY (`player_id`) REFERENCES `players`(`id`) ON DELETE SET NULL,
            CONSTRAINT `fk_sales_payment` FOREIGN KEY (`payment_id`) REFERENCES `payments`(`id`) ON DELETE SET NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `sales`",
];
