package api

import "git.threesix.ai/jordan/slack5-1770541397/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 preferences endpoints")

	// Define reusable schemas
	spec.WithSchema("NotificationPreferences", openapi.Object(map[string]openapi.Schema{
		"email": openapi.Bool().WithDescription("Email notifications enabled"),
		"push":  openapi.Bool().WithDescription("Push notifications enabled"),
		"sms":   openapi.Bool().WithDescription("SMS notifications enabled"),
	}))

	spec.WithSchema("Preferences", openapi.Object(map[string]openapi.Schema{
		"theme":         openapi.StringEnum("light", "dark", "system").WithDescription("UI theme preference"),
		"language":      openapi.StringWithMinMax(0, 10).WithDescription("BCP-47 language tag").WithExample("en"),
		"notifications": openapi.Ref("NotificationPreferences").WithDescription("Notification settings"),
	}))

	spec.WithSchema("UserPreferencesResponse", openapi.Object(map[string]openapi.Schema{
		"user_id":     openapi.String().WithDescription("User identifier"),
		"preferences": openapi.Ref("Preferences").WithDescription("User preferences"),
		"updated_at":  openapi.DateTime().WithDescription("Last update timestamp"),
	}, "user_id", "preferences", "updated_at"))

	spec.WithSchema("PutPreferencesRequest", openapi.Object(map[string]openapi.Schema{
		"preferences": openapi.Ref("Preferences").WithDescription("Preferences to save"),
	}, "preferences"))

	// user_id path parameter
	userIDParam := openapi.PathParam("user_id", "User 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 the user's preferences. Returns defaults if none have been saved. Requires authentication.",
		"tags":        []string{"Preferences"},
		"security":    []map[string][]string{{"bearer": {}}},
		"parameters":  []any{userIDParam},
		"responses": map[string]any{
			"200": openapi.OpResponse("Success", openapi.ResponseSchema(openapi.Ref("UserPreferencesResponse"))),
			"401": openapi.OpResponse("Unauthorized", openapi.ErrorResponseSchema()),
			"403": openapi.OpResponse("Forbidden", openapi.ErrorResponseSchema()),
		},
	})

	// Put preferences
	spec.AddPath("/api/preferences-api/preferences/{user_id}", "put", map[string]any{
		"summary":     "Set user preferences",
		"description": "Creates or replaces the user's preferences. Requires authentication.",
		"tags":        []string{"Preferences"},
		"security":    []map[string][]string{{"bearer": {}}},
		"parameters":  []any{userIDParam},
		"requestBody": openapi.RequestBody(openapi.Ref("PutPreferencesRequest"), true),
		"responses": map[string]any{
			"200": openapi.OpResponse("Success", openapi.ResponseSchema(openapi.Ref("UserPreferencesResponse"))),
			"400": openapi.OpResponse("Bad request", openapi.ErrorResponseSchema()),
			"401": openapi.OpResponse("Unauthorized", openapi.ErrorResponseSchema()),
			"403": openapi.OpResponse("Forbidden", openapi.ErrorResponseSchema()),
		},
	})

	return spec
}
