<?php
session_start();
include '../db.php';
include '../financial_helpers.php';

header('Content-Type: application/json');

$store_id = $_SESSION['store_id'] ?? 1;

if (!isset($_GET['id'])) {
    echo json_encode(['success' => false, 'message' => 'Account ID is required']);
    exit;
}

$account_id = (int)$_GET['id'];

try {
    $stmt = $conn->prepare("
        SELECT 
            id, account_code, account_name, account_type, account_category,
            parent_account_id, description, opening_balance, current_balance,
            is_active, created_at, updated_at
        FROM chart_of_accounts 
        WHERE id = ? AND store_id = ?
    ");
    $stmt->bind_param("ii", $account_id, $store_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $account = $result->fetch_assoc();
        echo json_encode(['success' => true, 'account' => $account]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Account not found']);
    }
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>
