<?php

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

$body = readJsonBody();
$id = isset($body['id']) ? trim((string) $body['id']) : '';
$enabled = !empty($body['enabled']);
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);
}

$chore = $chores[$idx];
$nfcMeta = normalizeChoreNfcMeta($chore['nfc'] ?? null);
if ((string) ($nfcMeta['token_hash'] ?? '') === '' && $enabled) {
    sendJson(['success' => false, 'error' => 'Generate an NFC token first'], 400);
}
$nfcMeta['enabled'] = $enabled;
$chore['nfc'] = $nfcMeta;
$chores[$idx] = migrateLegacyChoreRow($chore, $people);

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

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