import type { TemplateContext } from "./context";

export function generateReadme(context: TemplateContext, templateName: string) {
	const config = context.config;

	const ports = {
		rivetEngineGuard: 6420,
		runner: 5050,
		//nats: 4222,
		postgres: 5432,
		clickhouseHttp: 9300,
		clickhouseNative: 9301,
		otelGrpc: 4317,
		otelHttp: 4318,
	};

	const readmeContent = `# ${templateName} - Auto-generated Docker Compose Template

> ! **Auto-generated**: This directory and its contents are automatically generated by \`docker/template/\`. Do not edit these files directly as your changes will be overwritten.

## Overview

This Docker Compose configuration provides a complete development environment for Rivet with the following services:

- **Rivet Engine**: Main orchestration service
- **Rivet Shell**: Interactive shell for debugging
- **Runner**: Executes user code
- **ClickHouse**: Analytics and time-series database
- **PostgreSQL**: Relational database
- **Vector Server**: Log aggregation and processing
- **OpenTelemetry Collector**: Observability data collection

## Port Configuration

| Service | Port(s) | Description |
|---------|---------|-------------|
| Rivet Engine | ${ports.rivetEngineGuard} | Public endpoint |
| Runner | ${ports.runner} | Code execution service |
| PostgreSQL | ${ports.postgres} | Database |
| ClickHouse HTTP | ${ports.clickhouseHttp} | Database HTTP interface |
| ClickHouse Native | ${ports.clickhouseNative} | Database native protocol |
| OpenTelemetry gRPC | ${ports.otelGrpc} | OTLP gRPC endpoint |
| OpenTelemetry HTTP | ${ports.otelHttp} | OTLP HTTP endpoint |

## Template Configuration

**Template Name**: \`${templateName}\`  
**Base Port**: \`6420\`  
**Network Mode**: \`${config.networkMode}\`

### Datacenters
${config.datacenters.map((dc) => `- **${dc.id}**: ${dc.engines} engine(s), ${dc.runners} runner(s)`).join("\n")}

## Usage

1. Start all services:
   \`\`\`bash
   docker-compose up -d
   \`\`\`

2. Check service health:
   \`\`\`bash
   docker-compose ps
   \`\`\`

3. View logs:
   \`\`\`bash
   docker-compose logs -f [service-name]
   \`\`\`

4. Stop all services:
   \`\`\`bash
   docker-compose down
   \`\`\`

## Generated Files

This template generates the following files and directories:
- \`docker-compose.yml\` - Main Docker Compose configuration
- \`core/\` - Core services shared across datacenters:
  - \`clickhouse/\` - ClickHouse configuration and initialization
  - \`vector-server/\` - Vector aggregator configuration  
  - \`otel-collector-server/\` - OpenTelemetry Collector server configuration
- \`datacenters/\` - Datacenter-specific configurations:
${config.datacenters
	.map(
		(dc) => `  - \`${dc.id}/\` - Configuration for datacenter ${dc.id}:
    - \`postgres/\` - PostgreSQL setup scripts
    - \`rivet-engine/\` - Rivet Engine configuration
    - \`vector-client/\` - Vector client configuration
    - \`otel-collector-client/\` - OpenTelemetry Collector client configuration`,
	)
	.join("\n")}
- \`README.md\` - This file
`;

	context.writeFile("README.md", readmeContent);
}
