<?php

require_once __DIR__ . '/../includes/api_bootstrap.php';
require_once __DIR__ . '/../includes/chore_helpers.php';
require_once __DIR__ . '/../includes/family_settings.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 manage NFC'], 403);
}

$body = readJsonBody();
$id = isset($body['id']) ? trim((string) $body['id']) : '';
if ($id === '') {
    sendJson(['success' => false, 'error' => 'id is required'], 400);
}

$rawChores = normalizeChoresList(readJsonFile('chores.json'));
$chores = migrateAllChores($rawChores, $people);
$idx = findChoreIndexById($chores, $id);
if ($idx === null) {
    sendJson(['success' => false, 'error' => 'Chore not found'], 404);
}

$choreToken = generateOpaqueToken();
$chore = $chores[$idx];
$nfcMeta = normalizeChoreNfcMeta($chore['nfc'] ?? null);
$nfcMeta['token_hash'] = hashOpaqueToken($choreToken);
$nfcMeta['enabled'] = true;
$nfcMeta['created_at'] = gmdate('c');
$chore['nfc'] = $nfcMeta;
$chores[$idx] = migrateLegacyChoreRow($chore, $people);

$updatedPeople = $people;
$links = [];
$familySettings = loadFamilySettings();
$base = familyHubAppUrl($familySettings) . '/api/chore_submit_nfc.php';

foreach ($updatedPeople as $pi => $person) {
    $personId = (string) ($person['id'] ?? '');
    if ($personId === '') {
        continue;
    }
    $personName = (string) ($person['name'] ?? $personId);
    $personToken = generateOpaqueToken();
    $updatedPeople[$pi]['nfc_submit_token_hash'] = hashOpaqueToken($personToken);
    $updatedPeople[$pi]['nfc_submit_token_updated_at'] = gmdate('c');
    $links[] = [
        'person_id' => $personId,
        'person_name' => $personName,
        'url' => $base . '?id=' . rawurlencode($id) . '&token=' . rawurlencode($choreToken) . '&person_token=' . rawurlencode($personToken),
    ];
}

if (!writeJsonFile('people.json', $updatedPeople)) {
    sendJson(['success' => false, 'error' => 'Failed to save person NFC tokens'], 500);
}
if (!writeJsonFile('chores.json', $chores)) {
    sendJson(['success' => false, 'error' => 'Failed to save chore NFC token'], 500);
}

sendJson([
    'success' => true,
    'enabled' => true,
    'links' => $links,
    'nfc' => $nfcMeta,
]);
