import { join } from 'node:path';
import { OFFICER_ROOT } from '../data-path';

// Where the app store puts the containers it provisions.
//
// ── The install layout ──
//
// A machine that runs Officer is meant to look like this, whether the owner is seasoned or not:
//
//   ~/officerdev/
//     platform/     the app
//     data/         DATA_PATH — managed homes, attachments, job logs
//     dockers/      services the app store provisioned          <- this file
//     capabilities/ the file-based item store
//
// One root, everything under it, nothing scattered. `OFFICER_ROOT` is derived from `DATA_PATH` rather
// than configured separately, because a second environment variable that must agree with the first is a
// second thing to get wrong — and on a correct install `data/` is always a direct child of the root.
//
// ── Why this is not `~/dockers` ──
//
// That is where a seasoned user already keeps their own estate — 47 services on this machine alone. Two
// reasons to stay out of it:
//
//  1. **Isolation.** Containers the app store created and containers the user manages must be
//     distinguishable without inspecting them. A separate root makes that structural rather than a
//     naming convention we would have to enforce and they could break.
//  2. **We never reason about someone else's compose files.** The app store does not scan, adopt or
//     modify anything outside its own directory. "I already have one of these" is answered by the user
//     giving a URL (`mode: 'existing'`), never by us finding a directory and guessing it is theirs.
//
// So this directory is exclusively ours to write, and everything in it was put there by an install.

/**
 * The install root — on a conventional install, `~/officerdev`.
 *
 * Derived, not configured: see above. It moved to `../data-path` on 2026-08-12, when the direction
 * inverted — it used to be `dirname(DATA_PATH)`, back when DATA_PATH was the environment variable that
 * anchored everything. Re-exported here because this file is where callers expect to find it.
 */
export { OFFICER_ROOT };

/** Where provisioned services live, one directory each. Created on first install, not at boot. */
export const DOCKERS_DIR = join(OFFICER_ROOT, 'dockers');

/**
 * This service's own directory: `<root>/dockers/<id>/`, holding `docker-compose.yaml` and — because the
 * templates use relative bind mounts rather than named volumes — its data and configuration too.
 *
 * That is the convention the owner already uses everywhere: `./data`, `./database`, `./storage` beside
 * the compose file, so both the app and a human can see exactly what a service is keeping and where.
 * A named volume hides it behind `docker volume inspect`, which is the opposite of the point.
 */
export const serviceDir = (sidecarId: string): string => join(DOCKERS_DIR, sidecarId);

/** The compose file the installer renders and `docker compose` is run against. */
export const composeFile = (sidecarId: string): string => join(serviceDir(sidecarId), 'docker-compose.yaml');
