using detect.gui.Models.Entities;
using detect.gui.Services.Detect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Splat;

namespace detect.gui.Api.System;

public class LogApi
{
    public LogApi(IEndpointRouteBuilder? webApp)
    {
        // all
        webApp?.MapGet("/v1/system/log/all", () => Locator.Current.GetService<DetectLogService>()!.ListAll());
        
        // search
        webApp?.MapGet("/v1/system/log/search", ([FromQuery] int? userId, [FromQuery] string? description, [FromQuery]int pageNum = 1, [FromQuery]int pageSize = 10) => 
            Locator.Current.GetService<DetectLogService>()!.Search(userId, description, pageNum, pageSize));
        
        // one
        webApp?.MapGet("/v1/system/log/one", ([FromQuery] int? userId, [FromQuery] string? description) => 
            Locator.Current.GetService<DetectLogService>()!.ListOne(userId, description));
        
        // id
        webApp?.MapGet("/v1/system/log/{id:long}", ([FromRoute] long id) => 
            Locator.Current.GetService<DetectLogService>()!.ListById(id));
        
        // add
        webApp?.MapPost("/v1/system/log/", ([FromBody] LogEntity entity) => 
            Locator.Current.GetService<DetectLogService>()!.AddData(entity));
        
        // update
        webApp?.MapPut("/v1/system/log/", ([FromBody] LogEntity entity) => 
            Locator.Current.GetService<DetectLogService>()!.UpdateData(entity));
            
        // delete
        webApp?.MapDelete("/v1/system/log/{id:long}", ([FromRoute] long id) => 
            Locator.Current.GetService<DetectLogService>()!.DeleteById(id));
    }
}