unit ValidocAPI;

interface

uses
  System.SysUtils,
  System.Classes,
  System.Net.HttpClient,
  System.Net.URLClient,
  System.JSON;

type
  TValidocAPI = class
  private
    FBaseURL : string;
    FAPIKey  : string;
    function PostJSON(const ARota: string; const AJSON: TJSONObject): string;
  public
    constructor Create(const ABaseURL, AAPIKey: string);
    function EnviarDocumentoPDF(
      const ANomeArquivo,
            AClienteNome,
            AWhatsApp,
            AEmail,
            ATipoDocumento,
            AArquivoBase64: string): string;
    function ConsultarStatus(const ADocumentoID: string): string;
    function ReenviarLink(const ADocumentoID: string): string;
  end;

implementation

constructor TValidocAPI.Create(const ABaseURL, AAPIKey: string);
begin
  inherited Create;
  FBaseURL := ABaseURL.TrimRight(['/']);
  FAPIKey  := AAPIKey;
end;

function TValidocAPI.PostJSON(const ARota: string; const AJSON: TJSONObject): string;
var
  LClient  : THTTPClient;
  LContent : TStringStream;
  LResp    : IHTTPResponse;
begin
  Result := '';

  LClient := THTTPClient.Create;
  try
    LClient.ContentType := 'application/json';
    LClient.Accept := 'application/json';
    LClient.CustomHeaders['X-API-Key'] := FAPIKey;

    LContent := TStringStream.Create(AJSON.ToJSON, TEncoding.UTF8);
    try
      LResp := LClient.Post(FBaseURL + ARota, LContent);
      Result := LResp.ContentAsString(TEncoding.UTF8);

      if (LResp.StatusCode < 200) or
         (LResp.StatusCode >= 300) then
        raise Exception.CreateFmt('VALIDOC HTTP %d: %s', [LResp.StatusCode, Result]);
    finally
      LContent.Free;
    end;
  finally
    LClient.Free;
  end;
end;

function TValidocAPI.EnviarDocumentoPDF(
  const ANomeArquivo,
        AClienteNome,
        AWhatsApp,
        AEmail,
        ATipoDocumento,
        AArquivoBase64: string): string;
var
  LJSON   : TJSONObject;
  LCanais : TJSONArray;
begin
  LJSON := TJSONObject.Create;
  try
    LCanais := TJSONArray.Create;
    LCanais.Add('WHATSAPP');
    LCanais.Add('EMAIL');

    LJSON.AddPair('cliente_nome', AClienteNome);
    LJSON.AddPair('documento_nome', ANomeArquivo);
    LJSON.AddPair('tipo_documento', ATipoDocumento);
    LJSON.AddPair('whatsapp', AWhatsApp);
    LJSON.AddPair('email', AEmail);
    LJSON.AddPair('arquivo_base64', AArquivoBase64);
    LJSON.AddPair('canais', LCanais);
    LJSON.AddPair('exigir_otp', TJSONBool.Create(True));

    Result := PostJSON('/documentos/enviar', LJSON);
  finally
    LJSON.Free;
  end;
end;

function TValidocAPI.ConsultarStatus(const ADocumentoID: string): string;
var
  LClient : THTTPClient;
  LResp   : IHTTPResponse;
begin
  LClient := THTTPClient.Create;
  try
    LClient.Accept := 'application/json';
    LClient.CustomHeaders['X-API-Key'] := FAPIKey;
    LResp := LClient.Get(FBaseURL + '/documentos/' + ADocumentoID + '/status');
    Result := LResp.ContentAsString(TEncoding.UTF8);
  finally
    LClient.Free;
  end;
end;

function TValidocAPI.ReenviarLink(const ADocumentoID: string): string;
var
  LJSON: TJSONObject;
begin
  LJSON := TJSONObject.Create;
  try
    Result := PostJSON('/documentos/' + ADocumentoID + '/reenviar', LJSON);
  finally
    LJSON.Free;
  end;
end;

end.
