<?php
require_once '../../config/db.php';
require_once '../../config/session.php';
require_once '../../config/response.php';

session_start_if_not_started();

if (!is_logged_in()) {
    send_response(401, false, 'অনুমতি নেই');
}

$id = (int)($_GET['id'] ?? 0);

if (!$id) {
    send_response(400, false, 'পার্টি ID দিন');
}

try {
    // পার্টির বিস্তারিত
    $stmt = $pdo->prepare(
        "SELECT
            id, name, phone, address,
            balance, gold_balance, note,
            is_customer, is_supplier, is_pledge,
            created_at
         FROM parties
         WHERE id = ?"
    );
    $stmt->execute([$id]);
    $party = $stmt->fetch();

    if (!$party) {
        send_response(404, false, 'পার্টি পাওয়া যায়নি');
    }

    // সর্বশেষ ৫টা লেনদেন
    $txnStmt = $pdo->prepare(
        "SELECT
            id, transaction_date,
            transaction_type, amount,
            note, created_at
         FROM cash_transactions
         WHERE reference_type = 'PARTY'
         AND reference_id = ?
         AND deleted_at IS NULL
         ORDER BY id DESC
         LIMIT 5"
    );
    $txnStmt->execute([$id]);
    $transactions = $txnStmt->fetchAll();

    send_response(200, true, 'সফল', [
        'party'        => $party,
        'transactions' => $transactions
    ]);

} catch (Exception $e) {
    send_response(500, false,
        'সার্ভার সমস্যা: ' . $e->getMessage());
}
?>