<?php
// File: /public_html/erp.jewellersbd.com/api/cash/cash_transaction_get.php
// Cash Module 2.0 — fetch a single cash transaction's raw fields for the edit modal
// GET ?id=123  → { id, transaction_date, category_id, direction, amount, note, party_id, party_name,
//                   account_name, voucher_no, reference_type, editable }
// Uses $pdo (from config/db.php). NEW FILE — read-only, changes nothing.
// 2খ UPDATE: now returns party_id + party_name so the edit modal can pre-fill the party search box.

header("Content-Type: application/json; charset=UTF-8");

require_once __DIR__ . '/../../config/session.php';
require_once __DIR__ . '/../../config/db.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

function ctg_send($ok, $msg, $data = null, $code = 200) {
    http_response_code($code);
    echo json_encode(["success" => $ok, "message" => $msg, "data" => $data], JSON_UNESCAPED_UNICODE);
    exit;
}

if (
    (!isset($_SESSION['user_id']) || empty($_SESSION['user_id'])) &&
    (!function_exists('is_logged_in') || !is_logged_in())
) {
    ctg_send(false, "লগইন প্রয়োজন", null, 401);
}

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) ctg_send(false, "id দিন", null, 422);

try {
    $st = $pdo->prepare("SELECT t.id, t.transaction_date, t.category_id, t.direction, t.amount, t.note,
                                t.reference_type, t.is_locked, t.is_deleted, t.voucher_no, t.party_id,
                                a.account_name,
                                p.name AS party_name, p.phone AS party_phone
                         FROM cash_transactions t
                         LEFT JOIN cash_accounts a ON a.id = t.cash_account_id
                         LEFT JOIN parties       p ON p.id = t.party_id
                         WHERE t.id = ? LIMIT 1");
    $st->execute([$id]);
    $t = $st->fetch();
    if (!$t) ctg_send(false, "লেনদেন পাওয়া যায়নি", null, 404);

    $editable = ((string)$t['reference_type'] === 'MANUAL'
                 && (int)$t['is_locked'] === 0
                 && (int)$t['is_deleted'] === 0);

    $partyDisplay = '';
    if (!empty($t['party_name'])) {
        $partyDisplay = $t['party_name'] . (!empty($t['party_phone']) ? (' — ' . $t['party_phone']) : '');
    }

    ctg_send(true, "ok", [
        "id"               => (int)$t['id'],
        "transaction_date" => $t['transaction_date'],
        "category_id"      => (int)$t['category_id'],
        "direction"        => $t['direction'],
        "amount"           => (float)$t['amount'],
        "note"             => $t['note'] ?? "",
        "party_id"         => $t['party_id'] !== null ? (int)$t['party_id'] : 0,
        "party_display"    => $partyDisplay,
        "account_name"     => $t['account_name'] ?? "",
        "voucher_no"       => $t['voucher_no'] ?? "",
        "reference_type"   => $t['reference_type'] ?? "",
        "editable"         => $editable
    ]);

} catch (Throwable $e) {
    ctg_send(false, "সার্ভার সমস্যা: " . $e->getMessage(), null, 500);
}
?>