<?php
declare(strict_types=1);

return [
    'up' => "
CREATE TABLE IF NOT EXISTS bill_of_materials (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    item_id INT UNSIGNED NOT NULL,
    bom_code VARCHAR(50) NOT NULL,
    version INT UNSIGNED NOT NULL DEFAULT 1,
    name_ar VARCHAR(255) NOT NULL,
    name_en VARCHAR(255) NULL,
    output_quantity DECIMAL(12,3) NOT NULL DEFAULT 1.000,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    notes TEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_bom_code (bom_code),
    KEY idx_item (item_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS bom_components (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    bom_id INT UNSIGNED NOT NULL,
    component_item_id INT UNSIGNED NOT NULL,
    quantity DECIMAL(12,3) NOT NULL,
    unit VARCHAR(50) NULL,
    waste_percentage DECIMAL(5,2) NOT NULL DEFAULT 0.00,
    is_optional TINYINT(1) NOT NULL DEFAULT 0,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    notes VARCHAR(500) NULL,
    KEY idx_bom (bom_id),
    KEY idx_component (component_item_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS item_units (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    item_id INT UNSIGNED NOT NULL,
    unit_name_ar VARCHAR(50) NOT NULL,
    unit_name_en VARCHAR(50) NULL,
    conversion_factor DECIMAL(12,4) NOT NULL DEFAULT 1.0000,
    is_base_unit TINYINT(1) NOT NULL DEFAULT 0,
    is_purchase_unit TINYINT(1) NOT NULL DEFAULT 0,
    is_sales_unit TINYINT(1) NOT NULL DEFAULT 0,
    barcode VARCHAR(50) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_item (item_id),
    KEY idx_base (item_id, is_base_unit)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS item_attributes (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name_ar VARCHAR(100) NOT NULL,
    name_en VARCHAR(100) NULL,
    attribute_type ENUM('color','size','material','weight','custom') NOT NULL DEFAULT 'custom',
    possible_values JSON NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS item_attribute_values (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    item_id INT UNSIGNED NOT NULL,
    attribute_id INT UNSIGNED NOT NULL,
    value VARCHAR(255) NOT NULL,
    KEY idx_item (item_id),
    KEY idx_attribute (attribute_id),
    UNIQUE KEY uk_item_attr (item_id, attribute_id, value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS item_variants (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    item_id INT UNSIGNED NOT NULL,
    variant_code VARCHAR(50) NOT NULL,
    variant_name_ar VARCHAR(255) NOT NULL,
    attributes JSON NOT NULL,
    sku VARCHAR(50) NULL,
    barcode VARCHAR(50) NULL,
    additional_cost DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_variant_code (variant_code),
    KEY idx_item (item_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sales_representatives (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    employee_id INT UNSIGNED NULL,
    code VARCHAR(20) NOT NULL,
    name_ar VARCHAR(255) NOT NULL,
    name_en VARCHAR(255) NULL,
    phone VARCHAR(20) NULL,
    email VARCHAR(100) NULL,
    commission_type ENUM('flat','percentage','tiered') NOT NULL DEFAULT 'percentage',
    commission_rate DECIMAL(5,2) NOT NULL DEFAULT 0.00,
    target_amount DECIMAL(15,2) NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NULL ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_code (code),
    KEY idx_employee (employee_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS sales_commissions (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    representative_id INT UNSIGNED NOT NULL,
    sale_id INT UNSIGNED NOT NULL,
    sale_amount DECIMAL(15,2) NOT NULL,
    commission_rate DECIMAL(5,2) NOT NULL,
    commission_amount DECIMAL(15,2) NOT NULL,
    period_month DATE NOT NULL,
    status ENUM('calculated','approved','paid') NOT NULL DEFAULT 'calculated',
    paid_date DATE NULL,
    payment_reference VARCHAR(100) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_rep (representative_id),
    KEY idx_sale (sale_id),
    KEY idx_period (period_month),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS customer_price_lists (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name_ar VARCHAR(100) NOT NULL,
    name_en VARCHAR(100) NULL,
    discount_type ENUM('percentage','fixed') NOT NULL DEFAULT 'percentage',
    discount_value DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    priority INT UNSIGNED NOT NULL DEFAULT 0,
    start_date DATE NULL,
    end_date DATE NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS customer_item_prices (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    member_id INT UNSIGNED NULL,
    price_list_id INT UNSIGNED NULL,
    item_id INT UNSIGNED NOT NULL,
    category_id INT UNSIGNED NULL,
    discount_type ENUM('percentage','fixed') NOT NULL DEFAULT 'percentage',
    discount_value DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    special_price DECIMAL(15,2) NULL,
    start_date DATE NULL,
    end_date DATE NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_member (member_id),
    KEY idx_item (item_id),
    KEY idx_price_list (price_list_id),
    KEY idx_category (category_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
    'down' => "
DROP TABLE IF EXISTS customer_item_prices;
DROP TABLE IF EXISTS customer_price_lists;
DROP TABLE IF EXISTS sales_commissions;
DROP TABLE IF EXISTS sales_representatives;
DROP TABLE IF EXISTS item_variants;
DROP TABLE IF EXISTS item_attribute_values;
DROP TABLE IF EXISTS item_attributes;
DROP TABLE IF EXISTS item_units;
DROP TABLE IF EXISTS bom_components;
DROP TABLE IF EXISTS bill_of_materials",
];
