<?php
declare(strict_types=1);

return [
    'up' => "
CREATE TABLE IF NOT EXISTS documentary_credits (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    lc_number VARCHAR(50) NOT NULL,
    issuing_bank_id INT UNSIGNED NOT NULL,
    beneficiary_supplier_id INT UNSIGNED NULL,
    beneficiary_name VARCHAR(255) NOT NULL,
    amount DECIMAL(15,2) NOT NULL,
    currency_code VARCHAR(3) NOT NULL DEFAULT 'EGP',
    margin_amount DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    margin_percentage DECIMAL(5,2) NOT NULL DEFAULT 0.00,
    opening_date DATE NOT NULL,
    expiry_date DATE NOT NULL,
    shipment_date DATE NULL,
    status ENUM('opened','shipped','documents_presented','partial_paid','paid','closed','cancelled') NOT NULL DEFAULT 'opened',
    terms TEXT NULL,
    purchase_order_id INT UNSIGNED NULL,
    vendor_invoice_id INT UNSIGNED NULL,
    margin_journal_id INT UNSIGNED NULL,
    payment_journal_id INT UNSIGNED NULL,
    total_expenses DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    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_lc_number (lc_number),
    KEY idx_bank (issuing_bank_id),
    KEY idx_supplier (beneficiary_supplier_id),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS lc_documents (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    lc_id INT UNSIGNED NOT NULL,
    document_type ENUM('invoice','packing_list','bill_of_lading','insurance','certificate_of_origin','inspection','other') NOT NULL,
    document_number VARCHAR(100) NULL,
    document_date DATE NULL,
    received_date DATE NULL,
    status ENUM('pending','received','verified','discrepant') NOT NULL DEFAULT 'pending',
    discrepancy_notes TEXT NULL,
    file_path VARCHAR(500) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_lc (lc_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS letters_of_guarantee (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    guarantee_number VARCHAR(50) NOT NULL,
    bank_account_id INT UNSIGNED NOT NULL,
    beneficiary_name VARCHAR(255) NOT NULL,
    guarantee_type ENUM('tender','performance','advance_payment','maintenance','customs','other') NOT NULL,
    amount DECIMAL(15,2) NOT NULL,
    currency_code VARCHAR(3) NOT NULL DEFAULT 'EGP',
    margin_amount DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    margin_percentage DECIMAL(5,2) NOT NULL DEFAULT 0.00,
    commission_rate DECIMAL(5,4) NOT NULL DEFAULT 0.0000,
    commission_amount DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    issue_date DATE NOT NULL,
    expiry_date DATE NOT NULL,
    renewal_date DATE NULL,
    status ENUM('requested','issued','active','released','called','expired','renewed') NOT NULL DEFAULT 'requested',
    related_contract VARCHAR(255) NULL,
    margin_journal_id INT UNSIGNED NULL,
    commission_journal_id INT UNSIGNED NULL,
    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_guarantee_number (guarantee_number),
    KEY idx_bank (bank_account_id),
    KEY idx_type (guarantee_type),
    KEY idx_status (status),
    KEY idx_expiry (expiry_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS asset_categories (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name_ar VARCHAR(100) NOT NULL,
    name_en VARCHAR(100) NULL,
    parent_id INT UNSIGNED NULL,
    depreciation_method ENUM('straight_line','declining_balance','sum_of_years','units_of_production') NOT NULL DEFAULT 'straight_line',
    default_useful_life_months INT UNSIGNED NOT NULL DEFAULT 60,
    default_salvage_percentage DECIMAL(5,2) NOT NULL DEFAULT 0.00,
    declining_rate DECIMAL(5,2) NULL,
    asset_account_id INT UNSIGNED NULL,
    depreciation_account_id INT UNSIGNED NULL,
    expense_account_id INT UNSIGNED NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_parent (parent_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS asset_revaluations (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    asset_id INT UNSIGNED NOT NULL,
    revaluation_date DATE NOT NULL,
    old_value DECIMAL(15,2) NOT NULL,
    new_value DECIMAL(15,2) NOT NULL,
    difference DECIMAL(15,2) NOT NULL,
    direction ENUM('upward','downward') NOT NULL,
    reason VARCHAR(500) NULL,
    appraiser_name VARCHAR(255) NULL,
    journal_entry_id INT UNSIGNED NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_asset (asset_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS asset_improvements (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    asset_id INT UNSIGNED NOT NULL,
    improvement_date DATE NOT NULL,
    description VARCHAR(500) NOT NULL,
    cost DECIMAL(15,2) NOT NULL,
    extends_life_months INT UNSIGNED NOT NULL DEFAULT 0,
    vendor_name VARCHAR(255) NULL,
    invoice_number VARCHAR(100) NULL,
    journal_entry_id INT UNSIGNED NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_asset (asset_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE asset_register ADD COLUMN category_id INT UNSIGNED NULL AFTER item_id;
ALTER TABLE asset_register ADD COLUMN insurance_policy VARCHAR(100) NULL AFTER site_location;
ALTER TABLE asset_register ADD COLUMN insurance_expiry DATE NULL AFTER insurance_policy;
ALTER TABLE asset_register ADD COLUMN insurance_amount DECIMAL(15,2) NULL AFTER insurance_expiry",
    'down' => "
ALTER TABLE asset_register DROP COLUMN insurance_amount;
ALTER TABLE asset_register DROP COLUMN insurance_expiry;
ALTER TABLE asset_register DROP COLUMN insurance_policy;
ALTER TABLE asset_register DROP COLUMN category_id;
DROP TABLE IF EXISTS asset_improvements;
DROP TABLE IF EXISTS asset_revaluations;
DROP TABLE IF EXISTS asset_categories;
DROP TABLE IF EXISTS letters_of_guarantee;
DROP TABLE IF EXISTS lc_documents;
DROP TABLE IF EXISTS documentary_credits",
];
