namespace Health.WebApi.Endpoints;

/// <summary>
/// 患者端随访 API
/// </summary>
public static class FollowUpEndpoints
{
    public static void MapFollowUpEndpoints(this WebApplication app)
    {
        var group = app.MapGroup("/api/follow-ups").RequireAuthorization();

        // 患者查看自己的随访列表
        group.MapGet("/", async (HttpContext http, AppDbContext db, CancellationToken ct) =>
        {
            var userId = GetUserId(http);
            var followUps = await db.FollowUps
                .Where(f => f.UserId == userId)
                .OrderByDescending(f => f.ScheduledAt)
                .Select(f => new
                {
                    f.Id,
                    f.Title,
                    f.DoctorName,
                    f.Department,
                    f.ScheduledAt,
                    f.Notes,
                    Status = f.Status.ToString(),
                    f.CreatedAt
                })
                .ToListAsync(ct);

            return Results.Ok(new { code = 0, data = followUps, message = (string?)null });
        });
    }

    private static Guid GetUserId(HttpContext http) =>
        Guid.TryParse(http.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value, out var id) ? id : Guid.Empty;
}
