<?php
/**
 * Setup Capital Proxy Account
 * 1. Updates accounts.type ENUM
 * 2. Creates Original Capital (System)
 * 3. Creates capital (Proxy bank_cash)
 */

header('Content-Type: text/plain; charset=utf-8');

try {
    $dsn = "mysql:host=localhost;dbname=naveed_trader_db;charset=utf8mb4";
    $db = new PDO($dsn, "root", "", [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4"
    ]);

    echo "Connected to database.\n";

    // 1. Update ENUM
    echo "Updating accounts.type ENUM...\n";
    $db->exec("ALTER TABLE accounts MODIFY COLUMN type ENUM('party', 'bank_cash', 'system_income', 'system_expense', 'capital') NOT NULL");

    // 2. Create Original Capital (System)
    echo "Creating Original Capital account...\n";
    $stmt = $db->prepare("INSERT INTO accounts (name, type, sub_type, opening_balance, balance_type, is_system) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->execute(['Original Capital (اصل سرمایہ)', 'capital', 'equity', 0.00, 'Cr', 1]);

    // 3. Create Proxy Capital (bank_cash)
    echo "Creating Proxy Capital account...\n";
    $stmt->execute(['capital (سرمایہ)', 'bank_cash', 'cash', 0.00, 'Dr', 0]);

    echo "\nSUCCESS! Capital accounts setup completed.\n";

} catch (PDOException $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}
