
List<Agreement> agrs = new()
{
    new Agreement() { Uuid= Guid.NewGuid(), Number = "ISO-13485", Date = new DateOnly(), Sum = 50000, IdRequest = Guid.Parse("7184fa5f-b786-4478-863a-99cc000eb752") }
};

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapGet("/", () =>
{
    return agrs.Select(r => new AgreementEntityDto()
    {
        Uuid = r.Uuid,
        Number = r.Number,
        Sum = r.Sum,
        Date = r.Date,
        IdRequest = r.IdRequest,
    });
})
.WithName("GetAgreements")
.WithOpenApi();

app.MapGet("/{uuid}", (Guid uuid) =>
{
    var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);
    if (agr == null)
        return Results.NotFound();
    return Results.Json(new AgreementEntityDto()
    {
        Uuid = agr.Uuid,
        Number = agr.Number,
        Sum = agr.Sum,
        Date = agr.Date,
        IdRequest = agr.IdRequest,
    });
})
.WithName("GetAgreementByGUID")
.WithOpenApi();

app.MapPost("/{number}/{date}/{sum}/{idRequest}", (string? number, DateOnly date, decimal sum, Guid idRequest) =>
{
    Guid NewGuid = Guid.NewGuid();
    agrs.Add(new Agreement() { Uuid = NewGuid, Number = (string)number, Date = (DateOnly)date, Sum = (decimal)sum, IdRequest = (Guid)idRequest });
   
    var agr = agrs.FirstOrDefault(r => r.Uuid == NewGuid);
    if (agr == null)
        return Results.NotFound();
    return Results.Json(new AgreementEntityDto()
    {
        Uuid = agr.Uuid,
        Number = agr.Number,
        Sum = agr.Sum,
        Date = agr.Date,
        IdRequest = agr.IdRequest,
    });
})
.WithName("PostAgreement")
.WithOpenApi();

app.MapPatch("/{uuid}/{number}/{date}/{sum}/{idRequest}", (Guid uuid, string ?number, DateOnly date, decimal sum, Guid idRequest) =>
{
    var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);
    if (agr == null)
        return Results.NotFound();
    if (number != ",") agr.Number = number;
    if (date != null)agr.Date = date;
    if (sum != agr.Sum && sum != 0) agr.Sum = sum;
    if (idRequest != agr.IdRequest) agr.IdRequest = idRequest;

    return Results.Json(new AgreementEntityDto()
    {
        Uuid = agr.Uuid,
        Number = agr.Number,
        Sum = agr.Sum,
        Date = agr.Date,
        IdRequest = agr.IdRequest,
    });
})
.WithName("UpdateAgreement")
.WithOpenApi();

app.MapDelete("/{uuid}", (Guid uuid) =>
{
    var agr = agrs.FirstOrDefault(r => r.Uuid == uuid);    
    if (agr == null)
        return Results.NotFound();
    agrs.Remove(agr);
    return Results.Json(new AgreementEntityDto()
    {
        Uuid = agr.Uuid,
        Number = agr.Number,
        Sum = agr.Sum,
        Date = agr.Date,
        IdRequest = agr.IdRequest,
    });
})
.WithName("DeleteAgreement")
.WithOpenApi();

app.MapGet("/Requests/", async () =>
{
    var httpClient = new HttpClient();
    var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8080/");

    return secondWorkerResponse.ToArray();
})
.WithName("GetRequests")
.WithOpenApi();

app.Run();

public class Agreement
{
    public Guid Uuid { get; set; }
    public string Number { get; set; } = string.Empty;
    public DateOnly Date { get; set; }
    public Guid IdRequest { get; set; }
    public decimal Sum { get; set; } = 0;    
}

public class AgreementEntityDto : Agreement { }

public class Request
{
    public Guid Uuid { get; set; }
    public string Title { get; set; } = string.Empty;
    public string SourceOfFunds { get; set; } = string.Empty;
    public decimal Sum { get; set; } = 0;
    public bool IsCompleted { get; set; }
}

public class RequestEntityDto : Request { }