package infra

import (
	"strings"
	"testing"
)

func TestGenerateComposeTraefik(t *testing.T) {
	t.Run("render con volume y multiples envs", func(t *testing.T) {
		cfg := ComposeTraefikConfig{
			ProjectName:  "kanban",
			ServiceName:  "kanban",
			BuildContext: "../../",
			Dockerfile:   "apps/kanban/Dockerfile",
			Port:         8421,
			VolumeName:   "kanban_data",
			EnvVars:      []string{"KANBAN_TOKEN", "SECRET_KEY"},
			Network:      "coolify",
		}
		got := GenerateComposeTraefik(cfg)

		checks := []string{
			"name: kanban",
			"services:",
			"  kanban:",
			"    build:",
			"      context: ../../",
			"      dockerfile: apps/kanban/Dockerfile",
			"    container_name: kanban",
			"    restart: unless-stopped",
			"    ports:",
			`      - "8421:8421"`,
			"    volumes:",
			"      - kanban_data:/data",
			"    environment:",
			"      - KANBAN_TOKEN=${KANBAN_TOKEN:-}",
			"      - SECRET_KEY=${SECRET_KEY:-}",
			"    networks:",
			"      - coolify",
			"\nvolumes:",
			"  kanban_data:",
			"\nnetworks:",
			"  coolify:",
			"    external: true",
		}
		for _, want := range checks {
			if !strings.Contains(got, want) {
				t.Errorf("missing %q in output:\n%s", want, got)
			}
		}
	})

	t.Run("render sin volume", func(t *testing.T) {
		cfg := ComposeTraefikConfig{
			ProjectName:  "myapp",
			ServiceName:  "myapp",
			BuildContext: ".",
			Dockerfile:   "Dockerfile",
			Port:         9000,
			VolumeName:   "",
			EnvVars:      []string{"API_KEY"},
			Network:      "coolify",
		}
		got := GenerateComposeTraefik(cfg)

		if strings.Contains(got, "volumes:") {
			t.Errorf("expected no 'volumes:' section when VolumeName is empty, got:\n%s", got)
		}
		if !strings.Contains(got, "networks:") {
			t.Errorf("expected 'networks:' section, got:\n%s", got)
		}
		if !strings.Contains(got, "      - API_KEY=${API_KEY:-}") {
			t.Errorf("expected env var passthrough, got:\n%s", got)
		}
	})

	t.Run("render sin envs", func(t *testing.T) {
		cfg := ComposeTraefikConfig{
			ProjectName:  "plain",
			ServiceName:  "plain",
			BuildContext: ".",
			Dockerfile:   "Dockerfile",
			Port:         8080,
			VolumeName:   "plain_data",
			EnvVars:      nil,
			Network:      "coolify",
		}
		got := GenerateComposeTraefik(cfg)

		if strings.Contains(got, "environment:") {
			t.Errorf("expected no 'environment:' section when EnvVars is nil, got:\n%s", got)
		}
	})

	t.Run("project name con guion", func(t *testing.T) {
		cfg := ComposeTraefikConfig{
			ProjectName:  "registry-api",
			ServiceName:  "registry_api",
			BuildContext: "../../",
			Dockerfile:   "apps/registry_api/Dockerfile",
			Port:         8420,
			VolumeName:   "registry_data",
			EnvVars:      []string{"REGISTRY_API_TOKEN"},
			Network:      "coolify",
		}
		got := GenerateComposeTraefik(cfg)

		if !strings.Contains(got, "name: registry-api") {
			t.Errorf("expected 'name: registry-api', got:\n%s", got)
		}
		if !strings.Contains(got, "container_name: registry_api") {
			t.Errorf("expected 'container_name: registry_api', got:\n%s", got)
		}
	})

	t.Run("snapshot YAML completo replica patron registry_api", func(t *testing.T) {
		cfg := ComposeTraefikConfig{
			ProjectName:  "registry-api",
			ServiceName:  "registry_api",
			BuildContext: "../../",
			Dockerfile:   "apps/registry_api/Dockerfile",
			Port:         8420,
			VolumeName:   "registry_data",
			EnvVars:      []string{"REGISTRY_API_TOKEN"},
			Network:      "coolify",
		}
		got := GenerateComposeTraefik(cfg)

		expected := `name: registry-api

services:
  registry_api:
    build:
      context: ../../
      dockerfile: apps/registry_api/Dockerfile
    container_name: registry_api
    restart: unless-stopped
    ports:
      - "8420:8420"
    volumes:
      - registry_data:/data
    environment:
      - REGISTRY_API_TOKEN=${REGISTRY_API_TOKEN:-}
    networks:
      - coolify

volumes:
  registry_data:

networks:
  coolify:
    external: true
`
		if got != expected {
			t.Errorf("snapshot mismatch.\nGOT:\n%s\nWANT:\n%s", got, expected)
		}
	})
}
