<?php
// ═══════════════════════════════════════════════════════════════════════════════════════════════
// PURPOSE
//   Partnership settings reader. Returns the partnership-related field groups from every conf
//   file this host is allowed to see, so the partnership tab can render them as a form.
//
// OPERATIONAL MODEL
//   Read-only, and deliberately so. Saves from this form go to confform.php, which already
//   knows how to write a field change and push master.conf to partners. Duplicating that here
//   would mean two write paths for the same keys, and only one of them would push.
//
//   Which files are read is decided by the host, not the request. On HOST1 that is
//   master.conf's PARTNERSHIP section plus host1.conf's; on any other host, its own conf
//   alone — the same split sparse checkout enforces at git level.
//
// DESIGN PRINCIPLES
//   Filters full group parsing rather than pattern-matching the file.
//       vv_conf_all_groups() is used and then filtered by subsection, because it already
//       handles master.conf's sandwiched major header — the structure a naive section grep
//       gets wrong.
//
//   Matches the section name case-insensitively and by substring.
//       stripos, not equality, because the section is spelled PARTNERSHIP in master.conf and
//       Partnership in the host confs. Requiring an exact match would silently return one
//       file's groups and not the other's.
//
//   Files with no partnership section are omitted entirely.
//       The response lists only files that contributed, so the page renders one panel per
//       real section rather than an empty panel per readable file.
//
// OPERATIONAL SAFEGUARDS
//   Read-only. No parameters, no writes, nothing to validate — there is no input to this
//   endpoint at all, which is what makes the file list unforgeable.
//
//   The file list comes from vv_get_conf_files(), the same allowlist every conf endpoint uses,
//   so this cannot expose a partner's conf even though it enumerates rather than being told
//   what to read.
//
//   Never cached.
//       Cache-Control: no-store, no-cache — these are the values a user is actively editing,
//       and a cached read would show them their own change reverting.
//
//   Credential fields are returned as they appear in the conf.
//       The partnership sections carry SSH key paths and API keys, and this endpoint returns
//       them for editing. That is the same exposure the raw conf editor has, over the same
//       WebGUI session. This is a GET read, so Unraid's POST-only CSRF guard does not apply —
//       the session is its whole boundary. See README-unraid.md.
//
// REQUEST
//   GET, no parameters
//
// RESPONSE
//   {"ok":true,"files":[{"file":"<conf>","groups":[…]}, …]}
//
// DEPENDS ON
//   include/confform.php   vv_conf_all_groups()
//   include/config.php     vv_get_conf_files()
//   api/confform.php       the write path for these same fields
// ═══════════════════════════════════════════════════════════════════════════════════════════════
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache');
require_once dirname(__DIR__) . '/include/confform.php';

$files = vv_get_conf_files();
$out   = [];
foreach ($files as $f) {
    $groups = array_values(array_filter(
        vv_conf_all_groups($f),
        fn($g) => stripos($g['subsection'], 'partnership') !== false
    ));
    if ($groups) {
        $out[] = ['file' => $f, 'groups' => $groups];
    }
}

echo json_encode(['ok' => true, 'files' => $out]);
