<?php
require_once __DIR__ . '/helpers.php';


function ua_column_exists(mysqli $conn, string $table, string $column): bool {
    $db = $conn->query("SELECT DATABASE() AS db")->fetch_assoc();
    $dbName = $db['db'] ?? '';
    if ($dbName === '') return false;
    $stmt = $conn->prepare("SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=? AND TABLE_NAME=? AND COLUMN_NAME=? LIMIT 1");
    $stmt->bind_param('sss', $dbName, $table, $column);
    $stmt->execute();
    $res = $stmt->get_result();
    $exists = $res && $res->num_rows > 0;
    $stmt->close();
    return $exists;
}

function ua_ensure_column(mysqli $conn, string $table, string $column, string $definition): void {
    if (ua_column_exists($conn, $table, $column)) return;
    try { $conn->query("ALTER TABLE `$table` ADD COLUMN `$column` $definition"); }
    catch (Throwable $e) {}
}





function ensure_purchase_tables(mysqli $conn): void {
    $conn->query("CREATE TABLE IF NOT EXISTS shop_purchases (
        id INT AUTO_INCREMENT PRIMARY KEY,
        supplier_name VARCHAR(200) NULL,
        invoice_no VARCHAR(100) NULL,
        currency CHAR(3) NOT NULL DEFAULT 'SYP',
        usd_rate DECIMAL(12,2) NULL,
        total DECIMAL(12,2) NOT NULL DEFAULT 0,
        paid DECIMAL(12,2) NOT NULL DEFAULT 0,
        notes TEXT NULL,
        created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");

    // Ensure columns exist for older installations
    ua_ensure_column($conn, 'shop_purchases', 'supplier_name', 'VARCHAR(200) NULL');
    ua_ensure_column($conn, 'shop_purchases', 'invoice_no', 'VARCHAR(100) NULL');
    ua_ensure_column($conn, 'shop_purchases', 'currency', "CHAR(3) NOT NULL DEFAULT 'SYP'");
    ua_ensure_column($conn, 'shop_purchases', 'usd_rate', 'DECIMAL(12,2) NULL');
    ua_ensure_column($conn, 'shop_purchases', 'total', 'DECIMAL(12,2) NOT NULL DEFAULT 0');
    ua_ensure_column($conn, 'shop_purchases', 'paid', 'DECIMAL(12,2) NOT NULL DEFAULT 0');
    ua_ensure_column($conn, 'shop_purchases', 'notes', 'TEXT NULL');
    ua_ensure_column($conn, 'shop_purchases', 'created_at', 'DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP');
    ua_ensure_column($conn, 'shop_purchases', 'accounting_class', "VARCHAR(20) NOT NULL DEFAULT 'OPERATING'");
    ua_ensure_column($conn, 'shop_purchases', 'capital_allocation_mode', 'VARCHAR(12) NULL');
    ua_ensure_column($conn, 'shop_purchases', 'capital_partner_id', 'INT NULL');

    $conn->query("CREATE TABLE IF NOT EXISTS shop_purchase_items (
        id INT AUTO_INCREMENT PRIMARY KEY,
        purchase_id INT NOT NULL,
        item_name VARCHAR(200) NOT NULL,
        qty DECIMAL(12,2) NOT NULL DEFAULT 1,
        unit_price DECIMAL(12,2) NOT NULL DEFAULT 0,
        total DECIMAL(12,2) NOT NULL DEFAULT 0,
        created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        INDEX (purchase_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");

    ua_ensure_column($conn, 'shop_purchase_items', 'purchase_id', 'INT NOT NULL');
    ua_ensure_column($conn, 'shop_purchase_items', 'item_name', 'VARCHAR(200) NOT NULL');
    ua_ensure_column($conn, 'shop_purchase_items', 'qty', 'DECIMAL(12,2) NOT NULL DEFAULT 1');
    ua_ensure_column($conn, 'shop_purchase_items', 'unit_price', 'DECIMAL(12,2) NOT NULL DEFAULT 0');
    ua_ensure_column($conn, 'shop_purchase_items', 'total', 'DECIMAL(12,2) NOT NULL DEFAULT 0');
    ua_ensure_column($conn, 'shop_purchase_items', 'created_at', 'DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP');
}
