<?php
declare(strict_types=1);

return [
    'up' => "
CREATE TABLE IF NOT EXISTS movement_types (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    code VARCHAR(30) NOT NULL,
    name_ar VARCHAR(100) NOT NULL,
    name_en VARCHAR(100) NULL,
    module ENUM('sales','inventory','procurement') NOT NULL,
    direction ENUM('in','out','neutral') NOT NULL,
    affects_stock TINYINT(1) NOT NULL DEFAULT 1,
    posts_to_gl TINYINT(1) NOT NULL DEFAULT 1,
    requires_approval TINYINT(1) NOT NULL DEFAULT 0,
    creates_ar TINYINT(1) NOT NULL DEFAULT 0,
    creates_ap TINYINT(1) NOT NULL DEFAULT 0,
    allows_discount TINYINT(1) NOT NULL DEFAULT 1,
    allows_tax TINYINT(1) NOT NULL DEFAULT 1,
    party_types JSON NULL,
    auto_number_prefix VARCHAR(10) NULL,
    auto_number_next INT UNSIGNED NOT NULL DEFAULT 1,
    print_template VARCHAR(100) NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS movement_specifications (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    movement_type_id INT UNSIGNED NOT NULL,
    spec_key VARCHAR(50) NOT NULL,
    spec_value TEXT NULL,
    spec_type ENUM('boolean','string','integer','decimal','account','json') NOT NULL DEFAULT 'string',
    description_ar VARCHAR(255) NULL,
    KEY idx_movement_type (movement_type_id),
    UNIQUE KEY uk_type_key (movement_type_id, spec_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS currencies (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    code VARCHAR(3) NOT NULL,
    name_ar VARCHAR(50) NOT NULL,
    name_en VARCHAR(50) NULL,
    symbol VARCHAR(10) NOT NULL,
    decimal_places TINYINT UNSIGNED NOT NULL DEFAULT 2,
    is_base_currency TINYINT(1) NOT NULL DEFAULT 0,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS exchange_rates (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    currency_id INT UNSIGNED NOT NULL,
    rate_date DATE NOT NULL,
    buy_rate DECIMAL(15,6) NOT NULL,
    sell_rate DECIMAL(15,6) NOT NULL,
    mid_rate DECIMAL(15,6) NOT NULL,
    source VARCHAR(50) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_currency (currency_id),
    KEY idx_date (rate_date),
    UNIQUE KEY uk_currency_date (currency_id, rate_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE sales ADD COLUMN currency_code VARCHAR(3) NULL DEFAULT 'EGP' AFTER total_amount;
ALTER TABLE sales ADD COLUMN exchange_rate DECIMAL(15,6) NULL DEFAULT 1.000000 AFTER currency_code;
ALTER TABLE sales ADD COLUMN representative_id INT UNSIGNED NULL AFTER exchange_rate;

ALTER TABLE purchase_orders ADD COLUMN currency_code VARCHAR(3) NULL DEFAULT 'EGP' AFTER total_amount;
ALTER TABLE purchase_orders ADD COLUMN exchange_rate DECIMAL(15,6) NULL DEFAULT 1.000000 AFTER currency_code",
    'down' => "
ALTER TABLE purchase_orders DROP COLUMN exchange_rate;
ALTER TABLE purchase_orders DROP COLUMN currency_code;
ALTER TABLE sales DROP COLUMN representative_id;
ALTER TABLE sales DROP COLUMN exchange_rate;
ALTER TABLE sales DROP COLUMN currency_code;
DROP TABLE IF EXISTS exchange_rates;
DROP TABLE IF EXISTS currencies;
DROP TABLE IF EXISTS movement_specifications;
DROP TABLE IF EXISTS movement_types",
];
