using Clarity.Server.Services; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; namespace Clarity.Server.Endpoints { public static class ProfileEndpoints { public record OnboardingRequest(string FirstName, string? MiddleName, string LastName, string Ssn); public static IEndpointRouteBuilder MapProfileEndpoints(this IEndpointRouteBuilder app) { var group = app.MapGroup("/api/profile").RequireAuthorization(); group.MapGet("/", async (ClaimsPrincipal user, ProfileService svc, CancellationToken ct) => { var sub = user.FindFirstValue(ClaimTypes.NameIdentifier) ?? user.FindFirstValue("sub"); if (sub is null) return Results.Unauthorized(); var profile = await svc.GetBySubjectAsync(sub, ct); if (profile is null) return Results.NotFound(new { onboardingComplete = false }); return Results.Ok(profile); }); group.MapGet("/{subject}", async (string subject, ProfileService profileService) => { var profile = await profileService.GetBySubjectAsync(subject); if (profile == null) return Results.NotFound(new { message = "Profile not found!" }); return Results.Ok(profile); }); group.MapPost("/onboarding", async ( [FromBody] OnboardingRequest req, ClaimsPrincipal user, ProfileService svc, CancellationToken ct) => { var sub = user.FindFirstValue(ClaimTypes.NameIdentifier) ?? user.FindFirstValue("sub"); if (sub is null) return Results.Unauthorized(); var existing = await svc.GetBySubjectAsync(sub, ct); if (existing is not null) return Results.Conflict(new { message = "Profile already exists." }); var profile = await svc.CreateAsync(sub, req.FirstName, req.MiddleName, req.LastName, req.Ssn, ct); return Results.Ok(profile); }); return app; } } }