<?php

require_once __DIR__ . '/../includes/api_bootstrap.php';
require_once __DIR__ . '/../includes/family_settings.php';
require_once __DIR__ . '/../includes/banking_helpers.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    sendJson(['success' => false, 'error' => 'Method not allowed'], 405);
}

$people = migrateAllPeople(normalizePeopleList(readJsonFile('people.json')));
$actor = requireActivePerson($people);
if (($actor['role'] ?? '') !== ROLE_HEAD || !isHohVerified()) {
    sendJson(['success' => false, 'error' => 'Only a verified Head of household can adjust checking balances'], 403);
}
$settings = loadFamilySettings();
if (!bankingEnabled($settings)) {
    sendJson(['success' => false, 'error' => 'Banking mode is not enabled'], 400);
}

$people = bankingApplySavingsInterestToPeople($people, $settings);
$body = readJsonBody();
$personId = trim((string) ($body['person_id'] ?? ''));
$type = trim((string) ($body['type'] ?? ''));
$note = trim((string) ($body['note'] ?? ''));
$category = bankingCategoryOrDefault((string) ($body['category'] ?? ''), 'manual');
$amountRaw = $body['amount'] ?? null;

if ($personId === '') {
    sendJson(['success' => false, 'error' => 'person_id is required'], 400);
}
if (!in_array($type, ['credit', 'debit'], true)) {
    sendJson(['success' => false, 'error' => 'type must be credit or debit'], 400);
}
if (!is_numeric($amountRaw)) {
    sendJson(['success' => false, 'error' => 'amount must be numeric'], 400);
}
$amount = bankingRoundMoney((float) $amountRaw);
if ($amount <= 0) {
    sendJson(['success' => false, 'error' => 'amount must be greater than zero'], 400);
}

$targetIdx = null;
foreach ($people as $i => $p) {
    if (($p['id'] ?? '') === $personId) {
        $targetIdx = $i;
        break;
    }
}
if ($targetIdx === null) {
    sendJson(['success' => false, 'error' => 'Person not found'], 404);
}

$allocations = ['checking' => 0.0, 'savings' => 0.0, 'charity' => 0.0, 'roundup' => 0.0];
if ($type === 'credit') {
    $result = bankingCreditCheckingByRule($people[$targetIdx], $amount, $settings);
    $people[$targetIdx] = $result['person'];
    $allocations = $result['allocations'];
} else {
    $checking = (float) ($people[$targetIdx]['checking_balance'] ?? 0);
    if ($checking < $amount) {
        sendJson(['success' => false, 'error' => 'Insufficient checking balance'], 400);
    }
    $people[$targetIdx]['checking_balance'] = bankingRoundMoney($checking - $amount);
    $people[$targetIdx]['currency_balance'] = $people[$targetIdx]['checking_balance'];
    $allocations['checking'] = -$amount;
}

if (!writeJsonFile('people.json', $people)) {
    sendJson(['success' => false, 'error' => 'Failed to save people'], 500);
}

$tx = [
    'id' => bin2hex(random_bytes(8)),
    'type' => 'manual_' . $type,
    'person_id' => $personId,
    'amount' => $amount,
    'allocations' => $allocations,
    'category' => $category,
    'note' => $note,
    'created_at' => gmdate('c'),
    'created_by' => (string) ($actor['id'] ?? ''),
];
if (!appendBankTransaction($tx)) {
    sendJson(['success' => false, 'error' => 'Saved balances but failed to write transaction log'], 500);
}

sendJson(['success' => true, 'transaction' => $tx, 'person' => $people[$targetIdx]]);
