<?php
declare(strict_types=1);

return [
    'up' => "
CREATE TABLE IF NOT EXISTS supplier_price_quotes (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    quote_number VARCHAR(50) NOT NULL,
    requisition_id INT UNSIGNED NULL,
    supplier_id INT UNSIGNED NOT NULL,
    request_date DATE NOT NULL,
    response_date DATE NULL,
    expiry_date DATE NULL,
    status ENUM('requested','received','evaluated','accepted','rejected','expired') NOT NULL DEFAULT 'requested',
    delivery_terms VARCHAR(255) NULL,
    payment_terms VARCHAR(255) NULL,
    total_amount DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    currency VARCHAR(3) NOT NULL DEFAULT 'EGP',
    notes TEXT NULL,
    evaluation_id INT UNSIGNED 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_quote_number (quote_number),
    KEY idx_supplier (supplier_id),
    KEY idx_requisition (requisition_id),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS supplier_quote_items (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    quote_id INT UNSIGNED NOT NULL,
    item_id INT UNSIGNED NOT NULL,
    item_description VARCHAR(500) NULL,
    quantity DECIMAL(12,3) NOT NULL,
    unit_price DECIMAL(15,2) NOT NULL,
    total_price DECIMAL(15,2) NOT NULL,
    delivery_days INT UNSIGNED NULL,
    notes VARCHAR(500) NULL,
    KEY idx_quote (quote_id),
    KEY idx_item (item_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS quote_evaluations (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    evaluation_number VARCHAR(50) NOT NULL,
    requisition_id INT UNSIGNED NULL,
    evaluation_date DATE NOT NULL,
    status ENUM('draft','completed','approved') NOT NULL DEFAULT 'draft',
    winning_quote_id INT UNSIGNED NULL,
    justification TEXT NULL,
    purchase_order_id INT UNSIGNED NULL,
    evaluated_by INT UNSIGNED NULL,
    approved_by INT UNSIGNED NULL,
    approved_at DATETIME NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uk_eval_number (evaluation_number),
    KEY idx_requisition (requisition_id),
    KEY idx_winning_quote (winning_quote_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS supplier_payment_schedules (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    supplier_id INT UNSIGNED NOT NULL,
    invoice_id INT UNSIGNED NULL,
    schedule_number VARCHAR(50) NOT NULL,
    total_amount DECIMAL(15,2) NOT NULL,
    installments_count INT UNSIGNED NOT NULL,
    status ENUM('active','completed','cancelled') NOT NULL DEFAULT 'active',
    notes TEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_supplier (supplier_id),
    KEY idx_invoice (invoice_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS supplier_payment_schedule_items (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    schedule_id INT UNSIGNED NOT NULL,
    installment_number INT UNSIGNED NOT NULL,
    due_date DATE NOT NULL,
    amount DECIMAL(15,2) NOT NULL,
    payment_method ENUM('cash','check','transfer') NOT NULL DEFAULT 'cash',
    paid_amount DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    paid_date DATE NULL,
    status ENUM('upcoming','due','paid','overdue') NOT NULL DEFAULT 'upcoming',
    payment_id INT UNSIGNED NULL,
    KEY idx_schedule (schedule_id),
    KEY idx_due_date (due_date),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS item_price_history (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    item_id INT UNSIGNED NOT NULL,
    supplier_id INT UNSIGNED NULL,
    price_type ENUM('purchase','sale','standard') NOT NULL DEFAULT 'purchase',
    price DECIMAL(15,2) NOT NULL,
    effective_date DATE NOT NULL,
    document_type VARCHAR(50) NULL,
    document_id INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_item (item_id),
    KEY idx_supplier (supplier_id),
    KEY idx_item_supplier (item_id, supplier_id),
    KEY idx_date (effective_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
    'down' => "
DROP TABLE IF EXISTS item_price_history;
DROP TABLE IF EXISTS supplier_payment_schedule_items;
DROP TABLE IF EXISTS supplier_payment_schedules;
DROP TABLE IF EXISTS quote_evaluations;
DROP TABLE IF EXISTS supplier_quote_items;
DROP TABLE IF EXISTS supplier_price_quotes",
];
