package main

import (
	"fmt"
	"io"
)

func commandHelp(args []string, out io.Writer) (bool, error) {
	if len(args) == 0 {
		return false, nil
	}
	if len(args) == 1 && (args[0] == "--help" || args[0] == "-h" || args[0] == "help") {
		fmt.Fprint(out, `Usage: contentd [migrate]

Serve the content API, or apply its database schema explicitly.
Without a command, starts the HTTP server. Serving never runs migrations.

Commands:
  migrate          Apply embedded schema migrations transactionally, then exit.
  -h, --help       Show help without connecting to the database.

Server environment:
  DATABASE_URL     PostgreSQL URL or connection string.
                   Default: dbname=assignment_harness_native.
  APP_PROFILE      production (default) or compat for reference behavior tests.
  ALLOWED_HOSTS    Required in production: comma-separated exact hostnames.
                   Example: 127.0.0.1,localhost. No wildcards.
  LISTEN_ADDR      Bind address (default: 127.0.0.1:18001).
  DB_MAX_CONNS     Database connections (default: 8; range: 1-128).
  MAX_INFLIGHT     Production request limit (default: 64; range: 1-4096).
  SQL_TRACE        Trace output path; only supported in compat mode.

Production requires a migrated schema and exposes /healthz and /readyz.
Compat mode preserves reference errors and has no health endpoints.
PostgreSQL connections also honor PGHOST, PGPORT, PGUSER, and .pgpass.
Stop the server with Ctrl+C or SIGTERM for graceful shutdown.

Examples (from repository root):
  go-service/contentd migrate
  ALLOWED_HOSTS=127.0.0.1,localhost go-service/contentd
  APP_PROFILE=compat go-service/contentd

Use contentd migrate --help for migration details.
Test data and benchmarks belong to harness; see harness/harness --help.
`)
		return true, nil
	}
	if (len(args) == 2 && args[0] == "migrate" && (args[1] == "--help" || args[1] == "-h")) || (len(args) == 2 && args[0] == "help" && args[1] == "migrate") {
		fmt.Fprint(out, `Usage: contentd migrate

Apply the embedded schema to the database named by DATABASE_URL, then exit.
DATABASE_URL defaults to dbname=assignment_harness_native.
Server-only settings such as ALLOWED_HOSTS are not needed.

Writes schema and migration metadata using a transaction and advisory lock.
Repeated runs are safe. Refuses an existing untracked application schema.
Use a migration-owner role; serving should use a restricted runtime role.
This command does not create the database or load test data.

Example (from repository root):
  DATABASE_URL='dbname=content' go-service/contentd migrate

See deploy/README.md for deployment and role configuration.
`)
		return true, nil
	}
	if len(args) == 1 && args[0] == "migrate" {
		return false, nil
	}
	return true, fmt.Errorf("unexpected arguments; use contentd --help")
}
