package api

import "git.threesix.ai/jordan/slack5-1770544098/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 the preferences-api service").
		WithBearerSecurity("bearer", "JWT authentication token").
		WithTag("Health", "Service health endpoints").
		WithTag("Preferences", "User preferences endpoints")

	// Define reusable schemas
	spec.WithSchema("PreferencesResponse", 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").WithDescription("UI theme"),
			"language":              openapi.String().WithDescription("ISO 639-1 language code").WithPattern("^[a-z]{2}$").WithExample("en"),
			"notifications_enabled": openapi.Bool().WithDescription("Whether notifications are enabled"),
		}),
		"updated_at": openapi.Nullable(openapi.DateTime()).WithDescription("Last update timestamp"),
	}, "user_id", "preferences"))

	spec.WithSchema("UpdatePreferencesRequest", openapi.Object(map[string]openapi.Schema{
		"preferences": openapi.Object(map[string]openapi.Schema{
			"theme":                 openapi.StringEnum("light", "dark").WithDescription("UI theme"),
			"language":              openapi.String().WithDescription("ISO 639-1 language code").WithPattern("^[a-z]{2}$"),
			"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 user preferences
	spec.AddPath("/api/preferences-api/preferences/{user_id}", "get", map[string]any{
		"summary":     "Get user preferences",
		"description": "Returns all preferences for the authenticated user. Returns empty preferences for users with no saved preferences.",
		"tags":        []string{"Preferences"},
		"security":    []map[string][]string{{"bearer": {}}},
		"parameters": []any{
			openapi.PathParamWithSchema("user_id", "User ID (UUID)", openapi.UUID()),
		},
		"responses": map[string]any{
			"200": openapi.OpResponse("Success", openapi.ResponseSchema(openapi.Ref("PreferencesResponse"))),
			"400": openapi.OpResponse("Invalid user ID format", openapi.ErrorResponseSchema()),
			"401": openapi.OpResponse("Unauthorized", openapi.ErrorResponseSchema()),
			"403": openapi.OpResponse("Forbidden - user ID mismatch", openapi.ErrorResponseSchema()),
		},
	})

	// Update user preferences
	spec.AddPath("/api/preferences-api/preferences/{user_id}", "put", map[string]any{
		"summary":     "Update user preferences",
		"description": "Creates or updates preferences with upsert semantics. Only provided keys are changed; omitted keys are preserved.",
		"tags":        []string{"Preferences"},
		"security":    []map[string][]string{{"bearer": {}}},
		"parameters": []any{
			openapi.PathParamWithSchema("user_id", "User ID (UUID)", openapi.UUID()),
		},
		"requestBody": openapi.RequestBody(openapi.Ref("UpdatePreferencesRequest"), true),
		"responses": map[string]any{
			"200": openapi.OpResponse("Updated", openapi.ResponseSchema(openapi.Ref("PreferencesResponse"))),
			"400": openapi.OpResponse("Bad request - invalid key, value, or UUID", openapi.ErrorResponseSchema()),
			"401": openapi.OpResponse("Unauthorized", openapi.ErrorResponseSchema()),
			"403": openapi.OpResponse("Forbidden - user ID mismatch", openapi.ErrorResponseSchema()),
		},
	})

	return spec
}
