// Package stub generates server-side gRPC and HTTP stub servers
// from proto/swagger definitions. Stubs run as real k3s pods with DNS names.
package stub

import "fmt"

// Kind represents the type of stub to generate.
type Kind string

const (
	KindProto            Kind = "proto"
	KindSwagger          Kind = "swagger"
	KindPermissive       Kind = "permissive"
	KindAccountLocal     Kind = "account-local"
	KindDataplaneManager Kind = "dataplane-manager-local"
	KindMeteringRecorder Kind = "metering-recorder"
	KindBillingRecorder  Kind = "billing-recorder"
	KindRegionalAPIProxy Kind = "regional-api-proxy"
)

// Config holds configuration for generating a single service stub.
type Config struct {
	Service     string   // service name (e.g. "billing-svc")
	Kind        Kind     // proto, swagger, or permissive
	ProtoFiles  []string // paths to .proto files (for Kind=proto)
	SwaggerFile string   // path to swagger.json (for Kind=swagger)
	OutputDir   string   // directory to write generated code (.cp9/stubs/<service>/)
	GRPCPort    int      // gRPC listen port (default 50051)
	HTTPPort    int      // HTTP listen port (default 8080)
	Force       bool     // rebuild even if cache hit
}

// DefaultGRPCPort is the default port for gRPC stub servers.
const DefaultGRPCPort = 50051

// DefaultHTTPPort is the default port for HTTP stub servers.
const DefaultHTTPPort = 8080

// Result is the outcome of generating a stub.
type Result struct {
	Service   string `json:"service"`
	Kind      Kind   `json:"kind"`
	OutputDir string `json:"output_dir"`
	CacheKey  string `json:"cache_key"`
	Cached    bool   `json:"cached"`
}

// FormatText implements model.TextFormatter.
func (r Result) FormatText() string {
	if r.Cached {
		return fmt.Sprintf("stub %s: cached (kind=%s, key=%s)", r.Service, r.Kind, r.CacheKey[:12])
	}
	return fmt.Sprintf("stub %s: generated (kind=%s, dir=%s)", r.Service, r.Kind, r.OutputDir)
}

// ProtoServiceDef holds a parsed proto service definition for stub code generation.
type ProtoServiceDef struct {
	Package string
	Service string
	Methods []string
}

// SwaggerRoute holds a parsed swagger route for stub code generation.
type SwaggerRoute struct {
	Method     string // GET, POST, PUT, PATCH, DELETE
	Path       string // /api/v1/clusters
	StatusCode int    // 200, 201, etc.
	Response   string // JSON response body (empty string → "{}")
}
