package api

import "git.threesix.ai/jordan/slack5-1770529463/pkg/openapi"

// NewServiceSpec builds the OpenAPI specification for the preferences-api service.
func NewServiceSpec() *openapi.OpenAPISpec {
	spec := openapi.NewOpenAPISpec("preferences-api API", "1.0.0").
		WithDescription("REST API for managing user preferences").
		WithBearerSecurity("bearer", "JWT authentication token").
		WithTag("Health", "Service health endpoints").
		WithTag("Preferences", "User preference endpoints")

	// Define reusable schemas
	spec.WithSchema("UserPreferences", openapi.Object(map[string]openapi.Schema{
		"user_id": openapi.UUID().WithDescription("User identifier"),
		"preferences": openapi.Object(map[string]openapi.Schema{
			"theme":                 openapi.StringEnum("light", "dark", "system").WithDescription("UI theme").WithDefault("system"),
			"language":              openapi.String().WithDescription("BCP 47 language tag").WithExample("en"),
			"notifications_enabled": openapi.Bool().WithDescription("Whether notifications are enabled").WithDefault(true),
		}),
	}, "user_id", "preferences"))

	spec.WithSchema("UpdatePreferencesRequest", openapi.Object(map[string]openapi.Schema{
		"preferences": openapi.Object(map[string]openapi.Schema{
			"theme":                 openapi.StringEnum("light", "dark", "system").WithDescription("UI theme"),
			"language":              openapi.String().WithDescription("BCP 47 language tag").WithExample("fr"),
			"notifications_enabled": openapi.Bool().WithDescription("Whether notifications are enabled"),
		}),
	}, "preferences"))

	// Health
	spec.AddPath("/api/preferences-api/health", "get", map[string]any{
		"summary": "Health check",
		"tags":    []string{"Health"},
		"responses": map[string]any{
			"200": openapi.OpResponse("Service is healthy", openapi.Object(map[string]openapi.Schema{
				"service": openapi.String(),
				"status":  openapi.String(),
			})),
		},
	})

	// Get preferences
	spec.AddPath("/api/preferences-api/preferences/{user_id}", "get", map[string]any{
		"summary":     "Get user preferences",
		"description": "Returns all preferences for a user, merging stored values with server-defined defaults.",
		"tags":        []string{"Preferences"},
		"security":    []map[string][]string{{"bearer": {}}},
		"parameters":  []any{openapi.PathParamWithSchema("user_id", "User identifier (UUID)", openapi.UUID())},
		"responses": map[string]any{
			"200": openapi.OpResponse("Success", openapi.ResponseSchema(openapi.Ref("UserPreferences"))),
			"400": openapi.OpResponse("Invalid user_id format", openapi.ErrorResponseSchema()),
		},
	})

	// Update preferences
	spec.AddPath("/api/preferences-api/preferences/{user_id}", "put", map[string]any{
		"summary":     "Update user preferences",
		"description": "Creates or updates preferences for the given user. Only provided keys are updated; omitted keys retain their current value or default.",
		"tags":        []string{"Preferences"},
		"security":    []map[string][]string{{"bearer": {}}},
		"parameters":  []any{openapi.PathParamWithSchema("user_id", "User identifier (UUID)", openapi.UUID())},
		"requestBody": openapi.RequestBody(openapi.Ref("UpdatePreferencesRequest"), true),
		"responses": map[string]any{
			"200": openapi.OpResponse("Updated", openapi.ResponseSchema(openapi.Ref("UserPreferences"))),
			"400": openapi.OpResponse("Bad request (invalid user_id, unknown key, or invalid value)", openapi.ErrorResponseSchema()),
		},
	})

	return spec
}
