<?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);
$settings = loadFamilySettings();
if (!bankingEnabled($settings)) {
    sendJson(['success' => false, 'error' => 'Banking mode is not enabled'], 400);
}
$people = bankingApplySavingsInterestToPeople($people, $settings);
$body = readJsonBody();
$from = trim((string) ($body['from_account'] ?? ''));
$to = trim((string) ($body['to_account'] ?? ''));
$note = trim((string) ($body['note'] ?? ''));
$category = bankingCategoryOrDefault((string) ($body['category'] ?? ''), 'transfer');
$amountRaw = $body['amount'] ?? null;

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);
}
$allowedAccounts = ['checking', 'savings', 'charity'];
if (!in_array($from, $allowedAccounts, true) || !in_array($to, $allowedAccounts, true) || $from === $to) {
    sendJson(['success' => false, 'error' => 'Invalid transfer accounts'], 400);
}
if ($from === 'charity') {
    sendJson(['success' => false, 'error' => 'Funds in charity cannot be transferred out'], 400);
}

$actorId = (string) ($actor['id'] ?? '');
$idx = null;
foreach ($people as $i => $p) {
    if (($p['id'] ?? '') === $actorId) {
        $idx = $i;
        break;
    }
}
if ($idx === null) {
    sendJson(['success' => false, 'error' => 'Active person not found'], 404);
}

$fieldMap = [
    'checking' => 'checking_balance',
    'savings' => 'savings_balance',
    'charity' => 'charity_pending_balance',
];
$fromField = $fieldMap[$from];
$toField = $fieldMap[$to];
$fromBalance = (float) ($people[$idx][$fromField] ?? 0);
if ($fromBalance < $amount) {
    sendJson(['success' => false, 'error' => 'Insufficient source balance'], 400);
}
$people[$idx][$fromField] = bankingRoundMoney($fromBalance - $amount);
$people[$idx][$toField] = bankingRoundMoney((float) ($people[$idx][$toField] ?? 0) + $amount);
$people[$idx]['currency_balance'] = (float) ($people[$idx]['checking_balance'] ?? 0);

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

$tx = [
    'id' => bin2hex(random_bytes(8)),
    'type' => 'transfer',
    'person_id' => $actorId,
    'amount' => $amount,
    'from_account' => $from,
    'to_account' => $to,
    'category' => $category,
    'note' => $note,
    'created_at' => gmdate('c'),
    'created_by' => $actorId,
];
if (!appendBankTransaction($tx)) {
    sendJson(['success' => false, 'error' => 'Saved transfer but failed to write transaction log'], 500);
}

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