using Dapper;
using FastEndpoints;
using Npgsql;

namespace Modules.OMS.Lookups.Items;

public sealed class ResolveEndpoint(NpgsqlDataSource dataSource) : EndpointWithoutRequest
{
    public override void Configure()
    {
        Get("/api/lookups/items/{id}");
        Permissions("erp.item.read");
    }

    public override async Task HandleAsync(CancellationToken ct)
    {
        var id = Route<Guid>("id");
        await using var connection = await dataSource.OpenConnectionAsync(ct);
        var item = await connection.QuerySingleOrDefaultAsync(new CommandDefinition(@"
select id, code, name as DisplayName, case when is_active then '사용' else '중지' end as Status
from catalog.items where id=@Id;", new { Id = id }, cancellationToken: ct));
        if (item is null) { await Send.NotFoundAsync(ct); return; }
        await Send.OkAsync(item, ct);
    }
}
