package registry

import (
	"database/sql"
	"errors"
	"path/filepath"
	"testing"
	"time"
)

func openTestDB(t *testing.T) *sql.DB {
	t.Helper()
	db, err := Open(filepath.Join(t.TempDir(), "test.db"))
	if err != nil {
		t.Fatalf("open db: %v", err)
	}
	t.Cleanup(func() { _ = db.Close() })
	return db
}

func TestMigrationIdempotent(t *testing.T) {
	path := filepath.Join(t.TempDir(), "test.db")
	db1, err := Open(path)
	if err != nil {
		t.Fatalf("first open: %v", err)
	}
	_ = db1.Close()

	db2, err := Open(path)
	if err != nil {
		t.Fatalf("second open: %v", err)
	}
	_ = db2.Close()
}

func TestServiceCRUD(t *testing.T) {
	db := openTestDB(t)

	// Create
	if err := CreateService(db, "metacrypt", true); err != nil {
		t.Fatalf("create: %v", err)
	}

	// Get
	s, err := GetService(db, "metacrypt")
	if err != nil {
		t.Fatalf("get: %v", err)
	}
	if s.Name != "metacrypt" || !s.Active {
		t.Fatalf("got %+v", s)
	}

	// List
	services, err := ListServices(db)
	if err != nil {
		t.Fatalf("list: %v", err)
	}
	if len(services) != 1 || services[0].Name != "metacrypt" {
		t.Fatalf("list: got %d services", len(services))
	}

	// Update active
	if err := UpdateServiceActive(db, "metacrypt", false); err != nil {
		t.Fatalf("update: %v", err)
	}
	s, _ = GetService(db, "metacrypt")
	if s.Active {
		t.Fatal("expected inactive")
	}

	// Delete
	if err := DeleteService(db, "metacrypt"); err != nil {
		t.Fatalf("delete: %v", err)
	}
	_, err = GetService(db, "metacrypt")
	if !errors.Is(err, sql.ErrNoRows) {
		t.Fatalf("expected ErrNoRows after delete, got: %v", err)
	}
}

func TestServiceDuplicateName(t *testing.T) {
	db := openTestDB(t)
	if err := CreateService(db, "metacrypt", true); err != nil {
		t.Fatalf("first create: %v", err)
	}
	if err := CreateService(db, "metacrypt", true); err == nil {
		t.Fatal("expected error on duplicate name")
	}
}

func TestComponentCRUD(t *testing.T) {
	db := openTestDB(t)
	if err := CreateService(db, "metacrypt", true); err != nil {
		t.Fatalf("create service: %v", err)
	}

	// Create component
	c := &Component{
		Name:          "api",
		Service:       "metacrypt",
		Image:         "mcr.svc.mcp.metacircular.net:8443/metacrypt:v1.0.0",
		Network:       "docker_default",
		UserSpec:      "0:0",
		Restart:       "unless-stopped",
		DesiredState:  "running",
		ObservedState: "unknown",
		Ports:         []string{"127.0.0.1:18443:8443", "127.0.0.1:19443:9443"},
		Volumes:       []string{"/srv/metacrypt:/srv/metacrypt"},
		Cmd:           nil,
	}
	if err := CreateComponent(db, c); err != nil {
		t.Fatalf("create component: %v", err)
	}

	// Get
	got, err := GetComponent(db, "metacrypt", "api")
	if err != nil {
		t.Fatalf("get: %v", err)
	}
	if got.Image != c.Image {
		t.Fatalf("image: got %q, want %q", got.Image, c.Image)
	}
	if len(got.Ports) != 2 {
		t.Fatalf("ports: got %d, want 2", len(got.Ports))
	}
	if len(got.Volumes) != 1 {
		t.Fatalf("volumes: got %d, want 1", len(got.Volumes))
	}

	// Create a second component
	c2 := &Component{
		Name:          "web",
		Service:       "metacrypt",
		Image:         "mcr.svc.mcp.metacircular.net:8443/metacrypt-web:v1.0.0",
		Network:       "docker_default",
		Restart:       "unless-stopped",
		DesiredState:  "running",
		ObservedState: "unknown",
		Ports:         []string{"127.0.0.1:18080:8080"},
		Volumes:       []string{"/srv/metacrypt:/srv/metacrypt"},
		Cmd:           []string{"server", "--config", "/srv/metacrypt/metacrypt.toml"},
	}
	if err := CreateComponent(db, c2); err != nil {
		t.Fatalf("create web: %v", err)
	}

	// List
	components, err := ListComponents(db, "metacrypt")
	if err != nil {
		t.Fatalf("list: %v", err)
	}
	if len(components) != 2 {
		t.Fatalf("list: got %d, want 2", len(components))
	}

	// Verify cmd
	got2, _ := GetComponent(db, "metacrypt", "web")
	if len(got2.Cmd) != 3 || got2.Cmd[0] != "server" {
		t.Fatalf("cmd: got %v", got2.Cmd)
	}

	// Update state
	if err := UpdateComponentState(db, "metacrypt", "api", "", "running"); err != nil {
		t.Fatalf("update state: %v", err)
	}
	got, _ = GetComponent(db, "metacrypt", "api")
	if got.ObservedState != "running" {
		t.Fatalf("observed: got %q, want running", got.ObservedState)
	}
	if got.DesiredState != "running" {
		t.Fatalf("desired should be unchanged: got %q", got.DesiredState)
	}

	// Update spec
	c.Image = "mcr.svc.mcp.metacircular.net:8443/metacrypt:v2.0.0"
	c.Version = "v2.0.0"
	c.Ports = []string{"127.0.0.1:18443:8443"}
	if err := UpdateComponentSpec(db, c); err != nil {
		t.Fatalf("update spec: %v", err)
	}
	got, _ = GetComponent(db, "metacrypt", "api")
	if got.Image != c.Image {
		t.Fatalf("updated image: got %q", got.Image)
	}
	if len(got.Ports) != 1 {
		t.Fatalf("updated ports: got %d, want 1", len(got.Ports))
	}

	// Delete component
	if err := DeleteComponent(db, "metacrypt", "web"); err != nil {
		t.Fatalf("delete: %v", err)
	}
	components, _ = ListComponents(db, "metacrypt")
	if len(components) != 1 {
		t.Fatalf("after delete: got %d, want 1", len(components))
	}
}

func TestComponentCompositePK(t *testing.T) {
	db := openTestDB(t)
	if err := CreateService(db, "metacrypt", true); err != nil {
		t.Fatalf("create service: %v", err)
	}

	c := &Component{Name: "api", Service: "metacrypt", Image: "img:v1", Restart: "unless-stopped", DesiredState: "running", ObservedState: "unknown"}
	if err := CreateComponent(db, c); err != nil {
		t.Fatalf("first create: %v", err)
	}
	if err := CreateComponent(db, c); err == nil {
		t.Fatal("expected error on duplicate (service, name)")
	}
}

func TestCascadeDelete(t *testing.T) {
	db := openTestDB(t)
	if err := CreateService(db, "metacrypt", true); err != nil {
		t.Fatalf("create service: %v", err)
	}

	c := &Component{
		Name: "api", Service: "metacrypt", Image: "img:v1",
		Restart: "unless-stopped", DesiredState: "running", ObservedState: "unknown",
		Ports: []string{"8443:8443"}, Volumes: []string{"/srv/metacrypt:/srv/metacrypt"},
	}
	if err := CreateComponent(db, c); err != nil {
		t.Fatalf("create component: %v", err)
	}

	// Delete service should cascade to components, ports, volumes
	if err := DeleteService(db, "metacrypt"); err != nil {
		t.Fatalf("delete service: %v", err)
	}

	components, _ := ListComponents(db, "metacrypt")
	if len(components) != 0 {
		t.Fatalf("components should be empty after cascade, got %d", len(components))
	}
}

func TestComponentRoutes(t *testing.T) {
	db := openTestDB(t)
	if err := CreateService(db, "svc", true); err != nil {
		t.Fatalf("create service: %v", err)
	}

	// Create component with routes
	c := &Component{
		Name:          "api",
		Service:       "svc",
		Image:         "img:v1",
		Restart:       "unless-stopped",
		DesiredState:  "running",
		ObservedState: "unknown",
		Routes: []Route{
			{Name: "rest", Port: 8443, Mode: "l7", Hostname: "api.example.com"},
			{Name: "grpc", Port: 9443, Mode: "l4"},
		},
	}
	if err := CreateComponent(db, c); err != nil {
		t.Fatalf("create component: %v", err)
	}

	// Get and verify routes
	got, err := GetComponent(db, "svc", "api")
	if err != nil {
		t.Fatalf("get: %v", err)
	}
	if len(got.Routes) != 2 {
		t.Fatalf("routes: got %d, want 2", len(got.Routes))
	}
	// Routes are ordered by name: grpc, rest
	if got.Routes[0].Name != "grpc" || got.Routes[0].Port != 9443 || got.Routes[0].Mode != "l4" {
		t.Fatalf("route[0]: got %+v", got.Routes[0])
	}
	if got.Routes[1].Name != "rest" || got.Routes[1].Port != 8443 || got.Routes[1].Mode != "l7" || got.Routes[1].Hostname != "api.example.com" {
		t.Fatalf("route[1]: got %+v", got.Routes[1])
	}

	// Update routes via UpdateComponentSpec
	c.Routes = []Route{{Name: "http", Port: 8080, Mode: "l7"}}
	if err := UpdateComponentSpec(db, c); err != nil {
		t.Fatalf("update spec: %v", err)
	}
	got, _ = GetComponent(db, "svc", "api")
	if len(got.Routes) != 1 || got.Routes[0].Name != "http" {
		t.Fatalf("updated routes: got %+v", got.Routes)
	}

	// List components includes routes
	comps, err := ListComponents(db, "svc")
	if err != nil {
		t.Fatalf("list: %v", err)
	}
	if len(comps) != 1 || len(comps[0].Routes) != 1 {
		t.Fatalf("list routes: got %d components, %d routes", len(comps), len(comps[0].Routes))
	}
}

func TestRouteHostPort(t *testing.T) {
	db := openTestDB(t)
	if err := CreateService(db, "svc", true); err != nil {
		t.Fatalf("create service: %v", err)
	}

	c := &Component{
		Name:          "api",
		Service:       "svc",
		Image:         "img:v1",
		Restart:       "unless-stopped",
		DesiredState:  "running",
		ObservedState: "unknown",
		Routes: []Route{
			{Name: "rest", Port: 8443, Mode: "l7"},
			{Name: "grpc", Port: 9443, Mode: "l4"},
		},
	}
	if err := CreateComponent(db, c); err != nil {
		t.Fatalf("create component: %v", err)
	}

	// Initially host_port is 0
	ports, err := GetRouteHostPorts(db, "svc", "api")
	if err != nil {
		t.Fatalf("get host ports: %v", err)
	}
	if ports["rest"] != 0 || ports["grpc"] != 0 {
		t.Fatalf("initial host ports should be 0: %+v", ports)
	}

	// Update host ports
	if err := UpdateRouteHostPort(db, "svc", "api", "rest", 12345); err != nil {
		t.Fatalf("update rest: %v", err)
	}
	if err := UpdateRouteHostPort(db, "svc", "api", "grpc", 12346); err != nil {
		t.Fatalf("update grpc: %v", err)
	}

	ports, _ = GetRouteHostPorts(db, "svc", "api")
	if ports["rest"] != 12345 {
		t.Fatalf("rest host_port: got %d, want 12345", ports["rest"])
	}
	if ports["grpc"] != 12346 {
		t.Fatalf("grpc host_port: got %d, want 12346", ports["grpc"])
	}

	// Verify host_port is visible via GetComponent
	got, _ := GetComponent(db, "svc", "api")
	for _, r := range got.Routes {
		if r.Name == "rest" && r.HostPort != 12345 {
			t.Fatalf("GetComponent rest host_port: got %d", r.HostPort)
		}
		if r.Name == "grpc" && r.HostPort != 12346 {
			t.Fatalf("GetComponent grpc host_port: got %d", r.HostPort)
		}
	}

	// Update nonexistent route should fail
	err = UpdateRouteHostPort(db, "svc", "api", "nonexistent", 99999)
	if err == nil {
		t.Fatal("expected error updating nonexistent route")
	}
}

func TestRouteCascadeDelete(t *testing.T) {
	db := openTestDB(t)
	if err := CreateService(db, "svc", true); err != nil {
		t.Fatalf("create service: %v", err)
	}

	c := &Component{
		Name: "api", Service: "svc", Image: "img:v1",
		Restart: "unless-stopped", DesiredState: "running", ObservedState: "unknown",
		Routes: []Route{{Name: "rest", Port: 8443, Mode: "l4"}},
	}
	if err := CreateComponent(db, c); err != nil {
		t.Fatalf("create component: %v", err)
	}

	// Delete service cascades to routes
	if err := DeleteService(db, "svc"); err != nil {
		t.Fatalf("delete service: %v", err)
	}

	// Routes table should be empty
	ports, err := GetRouteHostPorts(db, "svc", "api")
	if err != nil {
		t.Fatalf("get routes after cascade: %v", err)
	}
	if len(ports) != 0 {
		t.Fatalf("routes should be empty after cascade, got %d", len(ports))
	}
}

func TestEvents(t *testing.T) {
	db := openTestDB(t)

	// Insert events
	if err := InsertEvent(db, "metacrypt", "api", "unknown", "running"); err != nil {
		t.Fatalf("insert: %v", err)
	}
	if err := InsertEvent(db, "metacrypt", "api", "running", "exited"); err != nil {
		t.Fatalf("insert: %v", err)
	}
	if err := InsertEvent(db, "mcr", "api", "unknown", "running"); err != nil {
		t.Fatalf("insert: %v", err)
	}

	// Query all recent
	events, err := QueryEvents(db, "", "", time.Now().Add(-1*time.Hour), 0)
	if err != nil {
		t.Fatalf("query: %v", err)
	}
	if len(events) != 3 {
		t.Fatalf("got %d events, want 3", len(events))
	}

	// Query by service
	events, _ = QueryEvents(db, "metacrypt", "", time.Now().Add(-1*time.Hour), 0)
	if len(events) != 2 {
		t.Fatalf("by service: got %d, want 2", len(events))
	}

	// Query by component
	events, _ = QueryEvents(db, "metacrypt", "api", time.Now().Add(-1*time.Hour), 0)
	if len(events) != 2 {
		t.Fatalf("by component: got %d, want 2", len(events))
	}

	// Query with limit
	events, _ = QueryEvents(db, "", "", time.Now().Add(-1*time.Hour), 1)
	if len(events) != 1 {
		t.Fatalf("with limit: got %d, want 1", len(events))
	}

	// Count
	count, err := CountEvents(db, "metacrypt", "api", time.Now().Add(-1*time.Hour))
	if err != nil {
		t.Fatalf("count: %v", err)
	}
	if count != 2 {
		t.Fatalf("count: got %d, want 2", count)
	}

	// Prune (prune nothing since events are recent)
	pruned, err := PruneEvents(db, time.Now().Add(-1*time.Hour))
	if err != nil {
		t.Fatalf("prune: %v", err)
	}
	if pruned != 0 {
		t.Fatalf("pruned: got %d, want 0", pruned)
	}

	// Prune everything
	pruned, err = PruneEvents(db, time.Now().Add(1*time.Hour))
	if err != nil {
		t.Fatalf("prune all: %v", err)
	}
	if pruned != 3 {
		t.Fatalf("pruned all: got %d, want 3", pruned)
	}
}
