package api

import "git.threesix.ai/jordan/slack5-1770603014/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 - user preferences management").
		WithTag("Health", "Service health endpoints").
		WithTag("Preferences", "User preferences endpoints")

	// Define reusable schemas
	notificationSettings := openapi.Object(map[string]openapi.Schema{
		"email":  openapi.Bool().WithDescription("Email notifications enabled"),
		"push":   openapi.Bool().WithDescription("Push notifications enabled"),
		"digest": openapi.StringEnum("daily", "weekly", "never").WithDescription("Digest frequency"),
	})

	preferencesSchema := openapi.Object(map[string]openapi.Schema{
		"theme":         openapi.StringEnum("light", "dark", "system").WithDescription("UI theme"),
		"language":      openapi.String().WithDescription("BCP 47 language tag"),
		"notifications": notificationSettings,
	})

	spec.WithSchema("PreferencesResponse", openapi.Object(map[string]openapi.Schema{
		"user_id":     openapi.UUID().WithDescription("User identifier"),
		"preferences": preferencesSchema,
		"updated_at":  openapi.DateTime().WithDescription("Last update timestamp"),
	}, "user_id", "preferences", "updated_at"))

	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"),
			"notifications": openapi.Object(map[string]openapi.Schema{
				"email":  openapi.Bool().WithDescription("Email notifications enabled"),
				"push":   openapi.Bool().WithDescription("Push notifications enabled"),
				"digest": openapi.StringEnum("daily", "weekly", "never").WithDescription("Digest frequency"),
			}),
		}),
	}, "preferences"))

	userIDParam := openapi.PathParam("user_id", "User UUID identifier")

	// 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 preferences for a user. Returns defaults if no preferences are stored.",
		"tags":        []string{"Preferences"},
		"parameters":  []any{userIDParam},
		"responses": map[string]any{
			"200": openapi.OpResponse("Success", openapi.ResponseSchema(openapi.Ref("PreferencesResponse"))),
			"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 a user. Performs a deep merge with existing preferences - only provided keys are changed.",
		"tags":        []string{"Preferences"},
		"parameters":  []any{userIDParam},
		"requestBody": openapi.RequestBody(openapi.Ref("UpdatePreferencesRequest"), true),
		"responses": map[string]any{
			"200": openapi.OpResponse("Success", openapi.ResponseSchema(openapi.Ref("PreferencesResponse"))),
			"400": openapi.OpResponse("Invalid request", openapi.ErrorResponseSchema()),
		},
	})

	return spec
}
