unit uDecaConfig;

interface

uses
  Winapi.Windows, System.SysUtils, System.Classes, System.IniFiles, System.Win.ComObj,
  Data.DB, FireDAC.Comp.Client, FireDAC.Stan.Param;

type
  TDecaModoConexion = (dmRemotoApi, dmLocalCli);

  TDecaConfig = record
    IdEmpresa: Integer;
    Modo: TDecaModoConexion;
    UrlServicio: string;      // ej: https://www.misdeca.es
    ApiKey: string;           // Token API de autenticación
    TimeoutSeg: Integer;      // Segundos de espera para peticiones HTTP / CLI
    RutaPhp: string;          // Ruta a ejecutable php.exe
    RutaScriptCli: string;    // Ruta a cli_emitir.php
    EmpresaCif: string;       // CIF de la empresa
    Activo: Boolean;          // Integración activa
  end;

function GetDecaIniFileName: string;
function GetDecaConfigPorDefecto: TDecaConfig;
function CargarDecaConfigEmpresa(AEmpresaId: Integer; out AConfig: TDecaConfig): Boolean;
function GuardarDecaConfigEmpresa(const AConfig: TDecaConfig): Boolean;
function CargarDecaConfig(out AConfig: TDecaConfig): Boolean;
function GuardarDecaConfig(const AConfig: TDecaConfig): Boolean;
function ProbarConexionDeca(const AConfig: TDecaConfig; out AInfoEmpresa, AErrorMsg: string): Boolean;
function EmitirDocumentoDeca(const AConfig: TDecaConfig; const APayloadJson: string; out ARespJson: string): Boolean;
function ConsultarDocumentoDeca(const AConfig: TDecaConfig; const ACodigo, AReferencia: string; out ARespJson: string): Boolean;
function ObtenerPresetsVehiculos(const AConfig: TDecaConfig; out ARespJson: string): Boolean;
function ObtenerPresetsContactos(const AConfig: TDecaConfig; const ATipo: string; out ARespJson: string): Boolean;
function GuardarPresetVehiculo(const AConfig: TDecaConfig; const APayloadJson: string): Boolean;
function GuardarPresetContacto(const AConfig: TDecaConfig; const APayloadJson: string): Boolean;

implementation

uses
  dmg_Main;

const
  DECA_CRYPT_KEY = 'DeCA_2026_Strato_ControlTime_SecKey#99!';
  DEFAULT_URL_SERVICIO = 'https://www.misdeca.es';
  DEFAULT_API_KEY = 'deca_live_d4f3c5022757c86719515056b017ac560284cc702831c7b0';
  DEFAULT_PHP_EXE = 'C:\xampp\php\php.exe';
  DEFAULT_SCRIPT_CLI = 'c:\fuentes\proyectos delphi\DECA\server\cli_emitir.php';
  DEFAULT_TIMEOUT_SEG = 30;

function CifrarCadena(const ATexto: string): string;
var
  i, kLen: Integer;
  b: Byte;
  kChar: Char;
  LHex: string;
begin
  Result := '';
  kLen := Length(DECA_CRYPT_KEY);
  if (ATexto = '') or (kLen = 0) then Exit;

  for i := 1 to Length(ATexto) do
  begin
    kChar := DECA_CRYPT_KEY[((i - 1) mod kLen) + 1];
    b := (Ord(ATexto[i]) xor Ord(kChar)) and $FF;
    LHex := IntToHex(b, 2);
    Result := Result + LHex;
  end;
end;

function DescifrarCadena(const AHex: string): string;
var
  i, kLen, bVal: Integer;
  kChar, rChar: Char;
  LSub: string;
begin
  Result := '';
  kLen := Length(DECA_CRYPT_KEY);
  if (Length(AHex) < 2) or ((Length(AHex) mod 2) <> 0) or (kLen = 0) then Exit;

  for i := 1 to Length(AHex) div 2 do
  begin
    LSub := '$' + Copy(AHex, (i - 1) * 2 + 1, 2);
    bVal := StrToIntDef(LSub, -1);
    if bVal < 0 then Exit;
    kChar := DECA_CRYPT_KEY[((i - 1) mod kLen) + 1];
    rChar := Chr(bVal xor Ord(kChar));
    Result := Result + rChar;
  end;
end;

function GetDecaIniFileName: string;
begin
  Result := ExtractFilePath(ParamStr(0)) + 'deca.ini';
end;

function GetDecaConfigPorDefecto: TDecaConfig;
begin
  Result.IdEmpresa := 0;
  Result.Modo := dmRemotoApi;
  Result.UrlServicio := DEFAULT_URL_SERVICIO;
  Result.ApiKey := DEFAULT_API_KEY;
  Result.TimeoutSeg := DEFAULT_TIMEOUT_SEG;
  Result.RutaPhp := DEFAULT_PHP_EXE;
  Result.RutaScriptCli := DEFAULT_SCRIPT_CLI;
  Result.EmpresaCif := '';
  Result.Activo := True;
end;

function CargarDecaConfigEmpresa(AEmpresaId: Integer; out AConfig: TDecaConfig): Boolean;
var
  LQry: TFDQuery;
begin
  Result := False;
  AConfig := GetDecaConfigPorDefecto;
  AConfig.IdEmpresa := AEmpresaId;

  // 1. Intentar cargar desde ge_deca_config en base de datos
  if (AEmpresaId > 0) and Assigned(dmgMain) and Assigned(dmgMain.dbConn) and dmgMain.dbConn.Connected then
  begin
    LQry := TFDQuery.Create(nil);
    try
      LQry.Connection := dmgMain.dbConn;
      LQry.SQL.Text := 'SELECT * FROM ge_deca_config WHERE id_empresa = :emp LIMIT 1';
      LQry.ParamByName('emp').AsInteger := AEmpresaId;
      try
        LQry.Open;
        if not LQry.IsEmpty then
        begin
          if SameText(LQry.FieldByName('modo').AsString, 'LOCAL_CLI') then
            AConfig.Modo := dmLocalCli
          else
            AConfig.Modo := dmRemotoApi;

          AConfig.UrlServicio := Trim(LQry.FieldByName('url_servicio').AsString);
          if AConfig.UrlServicio = '' then AConfig.UrlServicio := DEFAULT_URL_SERVICIO;

          AConfig.ApiKey := Trim(LQry.FieldByName('api_key').AsString);
          AConfig.TimeoutSeg := LQry.FieldByName('timeout_seg').AsInteger;
          if AConfig.TimeoutSeg <= 0 then AConfig.TimeoutSeg := DEFAULT_TIMEOUT_SEG;

          AConfig.RutaPhp := Trim(LQry.FieldByName('ruta_php').AsString);
          if AConfig.RutaPhp = '' then AConfig.RutaPhp := DEFAULT_PHP_EXE;

          AConfig.RutaScriptCli := Trim(LQry.FieldByName('ruta_script_cli').AsString);
          if AConfig.RutaScriptCli = '' then AConfig.RutaScriptCli := DEFAULT_SCRIPT_CLI;

          AConfig.Activo := (LQry.FieldByName('activo').AsInteger <> 0);

          // Cargar CIF de la empresa si es posible
          try
            dmgMain.qryExec.Close;
            dmgMain.qryExec.SQL.Text := 'SELECT cif_nif FROM ge_empresas WHERE id = :emp LIMIT 1';
            dmgMain.qryExec.ParamByName('emp').AsInteger := AEmpresaId;
            dmgMain.qryExec.Open;
            if not dmgMain.qryExec.IsEmpty then
              AConfig.EmpresaCif := Trim(dmgMain.qryExec.FieldByName('cif_nif').AsString);
            dmgMain.qryExec.Close;
          except
          end;

          // Si la API key está vacía, intentar autodescubrimiento desde deca.deca_empresas
          if (AConfig.ApiKey = '') and (AConfig.EmpresaCif <> '') then
          begin
            try
              dmgMain.qryExec.Close;
              dmgMain.qryExec.SQL.Text := 'SELECT api_key FROM deca.deca_empresas WHERE REPLACE(cif_nif, '' '', '''') = REPLACE(:cif, '' '', '''') LIMIT 1';
              dmgMain.qryExec.ParamByName('cif').AsString := AConfig.EmpresaCif;
              dmgMain.qryExec.Open;
              if not dmgMain.qryExec.IsEmpty and (Trim(dmgMain.qryExec.FieldByName('api_key').AsString) <> '') then
                AConfig.ApiKey := Trim(dmgMain.qryExec.FieldByName('api_key').AsString);
              dmgMain.qryExec.Close;
            except
            end;
          end;

          Result := True;
          Exit;
        end;
      except
        // Si falla la tabla ge_deca_config, continuar con fallback
      end;
    finally
      LQry.Free;
    end;

    // Si no había registro en ge_deca_config, intentar cargar directo de deca.deca_empresas según el CIF
    try
      dmgMain.qryExec.Close;
      dmgMain.qryExec.SQL.Text := 'SELECT cif_nif FROM ge_empresas WHERE id = :emp LIMIT 1';
      dmgMain.qryExec.ParamByName('emp').AsInteger := AEmpresaId;
      dmgMain.qryExec.Open;
      if not dmgMain.qryExec.IsEmpty then
        AConfig.EmpresaCif := Trim(dmgMain.qryExec.FieldByName('cif_nif').AsString);
      dmgMain.qryExec.Close;

      if AConfig.EmpresaCif <> '' then
      begin
        dmgMain.qryExec.SQL.Text := 'SELECT api_key FROM deca.deca_empresas WHERE REPLACE(cif_nif, '' '', '''') = REPLACE(:cif, '' '', '''') LIMIT 1';
        dmgMain.qryExec.ParamByName('cif').AsString := AConfig.EmpresaCif;
        dmgMain.qryExec.Open;
        if not dmgMain.qryExec.IsEmpty and (Trim(dmgMain.qryExec.FieldByName('api_key').AsString) <> '') then
        begin
          AConfig.ApiKey := Trim(dmgMain.qryExec.FieldByName('api_key').AsString);
          dmgMain.qryExec.Close;
          Result := True;
          Exit;
        end;
        dmgMain.qryExec.Close;
      end;
    except
    end;
  end;

  // 2. Fallback a deca.ini
  Result := CargarDecaConfig(AConfig);
  AConfig.IdEmpresa := AEmpresaId;
end;

function GuardarDecaConfigEmpresa(const AConfig: TDecaConfig): Boolean;
var
  LQry: TFDQuery;
  LModoStr: string;
begin
  Result := False;

  // 1. Guardar en base de datos ge_deca_config
  if (AConfig.IdEmpresa > 0) and Assigned(dmgMain) and Assigned(dmgMain.dbConn) and dmgMain.dbConn.Connected then
  begin
    if AConfig.Modo = dmLocalCli then
      LModoStr := 'LOCAL_CLI'
    else
      LModoStr := 'REMOTO_API';

    LQry := TFDQuery.Create(nil);
    try
      LQry.Connection := dmgMain.dbConn;
      LQry.SQL.Text :=
        'INSERT INTO ge_deca_config (id_empresa, modo, url_servicio, api_key, timeout_seg, ruta_php, ruta_script_cli, activo) ' +
        'VALUES (:emp, :modo, :url, :apikey, :timeout, :php, :cli, :act) ' +
        'ON DUPLICATE KEY UPDATE ' +
        '  modo = VALUES(modo), ' +
        '  url_servicio = VALUES(url_servicio), ' +
        '  api_key = VALUES(api_key), ' +
        '  timeout_seg = VALUES(timeout_seg), ' +
        '  ruta_php = VALUES(ruta_php), ' +
        '  ruta_script_cli = VALUES(ruta_script_cli), ' +
        '  activo = VALUES(activo)';

      LQry.ParamByName('emp').AsInteger         := AConfig.IdEmpresa;
      LQry.ParamByName('modo').AsWideString     := LModoStr;
      LQry.ParamByName('url').AsWideString      := Trim(AConfig.UrlServicio);
      LQry.ParamByName('apikey').AsWideString   := Trim(AConfig.ApiKey);
      LQry.ParamByName('timeout').AsInteger     := AConfig.TimeoutSeg;
      LQry.ParamByName('php').AsWideString      := Trim(AConfig.RutaPhp);
      LQry.ParamByName('cli').AsWideString      := Trim(AConfig.RutaScriptCli);
      LQry.ParamByName('act').AsInteger         := Ord(AConfig.Activo);
      try
        LQry.ExecSQL;
        Result := True;
      except
        // Silencioso si la BD falla
      end;
    finally
      LQry.Free;
    end;
  end;

  // 2. Sincronizar también a deca.ini
  GuardarDecaConfig(AConfig);
end;

function CargarDecaConfig(out AConfig: TDecaConfig): Boolean;
var
  LIniFile: string;
  LIni: TIniFile;
  LRawData, LDecrypted: string;
  LMemIni: TMemIniFile;
  LStrings: TStringList;
begin
  AConfig := GetDecaConfigPorDefecto;
  LIniFile := GetDecaIniFileName;

  if not FileExists(LIniFile) then
  begin
    GuardarDecaConfig(AConfig);
    Result := True;
    Exit;
  end;

  LIni := TIniFile.Create(LIniFile);
  try
    LRawData := LIni.ReadString('DECA_SECURE_CONFIG', 'Data', '');
  finally
    LIni.Free;
  end;

  if LRawData = '' then
  begin
    GuardarDecaConfig(AConfig);
    Result := True;
    Exit;
  end;

  LDecrypted := DescifrarCadena(LRawData);
  if LDecrypted = '' then
  begin
    Result := False;
    Exit;
  end;

  LStrings := TStringList.Create;
  try
    LStrings.Text := LDecrypted;
    LMemIni := TMemIniFile.Create('');
    try
      LMemIni.SetStrings(LStrings);
      if SameText(LMemIni.ReadString('CONEXION', 'Modo', 'REMOTO_API'), 'LOCAL_CLI') then
        AConfig.Modo := dmLocalCli
      else
        AConfig.Modo := dmRemotoApi;

      AConfig.UrlServicio := LMemIni.ReadString('CONEXION', 'UrlServicio', DEFAULT_URL_SERVICIO);
      AConfig.ApiKey := LMemIni.ReadString('CONEXION', 'ApiKey', DEFAULT_API_KEY);
      AConfig.TimeoutSeg := LMemIni.ReadInteger('CONEXION', 'TimeoutSeg', DEFAULT_TIMEOUT_SEG);
      AConfig.RutaPhp := LMemIni.ReadString('LOCAL', 'RutaPhp', DEFAULT_PHP_EXE);
      AConfig.RutaScriptCli := LMemIni.ReadString('LOCAL', 'RutaScriptCli', DEFAULT_SCRIPT_CLI);
      AConfig.EmpresaCif := LMemIni.ReadString('EMPRESA', 'Cif', '');
      AConfig.Activo := LMemIni.ReadBool('CONEXION', 'Activo', True);
      Result := True;
    finally
      LMemIni.Free;
    end;
  finally
    LStrings.Free;
  end;
end;

function GuardarDecaConfig(const AConfig: TDecaConfig): Boolean;
var
  LIniFile: string;
  LMemIni: TMemIniFile;
  LStrings: TStringList;
  LPlainText, LEncrypted: string;
  LDiskIni: TIniFile;
begin
  Result := False;
  LIniFile := GetDecaIniFileName;

  LStrings := TStringList.Create;
  try
    LMemIni := TMemIniFile.Create('');
    try
      if AConfig.Modo = dmLocalCli then
        LMemIni.WriteString('CONEXION', 'Modo', 'LOCAL_CLI')
      else
        LMemIni.WriteString('CONEXION', 'Modo', 'REMOTO_API');

      LMemIni.WriteString('CONEXION', 'UrlServicio', Trim(AConfig.UrlServicio));
      LMemIni.WriteString('CONEXION', 'ApiKey', Trim(AConfig.ApiKey));
      LMemIni.WriteInteger('CONEXION', 'TimeoutSeg', AConfig.TimeoutSeg);
      LMemIni.WriteString('LOCAL', 'RutaPhp', Trim(AConfig.RutaPhp));
      LMemIni.WriteString('LOCAL', 'RutaScriptCli', Trim(AConfig.RutaScriptCli));
      LMemIni.WriteString('EMPRESA', 'Cif', Trim(AConfig.EmpresaCif));
      LMemIni.WriteBool('CONEXION', 'Activo', AConfig.Activo);

      LMemIni.GetStrings(LStrings);
      LPlainText := LStrings.Text;
    finally
      LMemIni.Free;
    end;

    LEncrypted := CifrarCadena(LPlainText);

    LDiskIni := TIniFile.Create(LIniFile);
    try
      LDiskIni.WriteString('DECA_SECURE_CONFIG', 'Data', LEncrypted);
      LDiskIni.WriteString('DECA_SECURE_CONFIG', 'Version', '1.0');
      Result := True;
    finally
      LDiskIni.Free;
    end;
  finally
    LStrings.Free;
  end;
end;

function NormalizarUrlBase(const AUrl: string): string;
begin
  Result := Trim(AUrl);
  while (Length(Result) > 0) and (Result[Length(Result)] = '/') do
    Delete(Result, Length(Result), 1);
end;

function EnviarHttpRest(const AUrl, APayloadJson, AApiKey: string; ATimeoutSeg: Integer; out ARespJson: string): Boolean;
var
  LHttp: OleVariant;
  LStatus: Integer;
begin
  Result := False;
  ARespJson := '';
  try
    LHttp := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    LHttp.Open('POST', AUrl, False);
    LHttp.SetTimeouts(5000, 10000, ATimeoutSeg * 1000, ATimeoutSeg * 1000);
    LHttp.SetRequestHeader('Content-Type', 'application/json; charset=utf-8');
    if Trim(AApiKey) <> '' then
    begin
      LHttp.SetRequestHeader('X-API-Key', Trim(AApiKey));
      LHttp.SetRequestHeader('Authorization', 'Bearer ' + Trim(AApiKey));
    end;

    LHttp.Send(APayloadJson);
    LStatus := LHttp.Status;
    ARespJson := Trim(LHttp.ResponseText);

    // 200 = OK, 400 = Error de validacion de campos estructurado en JSON
    Result := (LStatus = 200) or ((LStatus = 400) and (Pos('"error"', ARespJson) > 0));
    if not Result then
    begin
      if ARespJson = '' then
        ARespJson := Format('El servidor retornó HTTP %d: %s', [LStatus, LHttp.StatusText]);
    end;
  except
    on E: Exception do
      ARespJson := 'Error de comunicación HTTPS: ' + E.Message;
  end;
end;

function ProbarConexionDeca(const AConfig: TDecaConfig; out AInfoEmpresa, AErrorMsg: string): Boolean;
var
  LUrlPing: string;
  LHttp: OleVariant;
  LStatus: Integer;
  LRespText: string;
  LPhpExe, LScriptFile: string;
begin
  Result := False;
  AInfoEmpresa := '';
  AErrorMsg := '';

  if AConfig.Modo = dmRemotoApi then
  begin
    if Trim(AConfig.UrlServicio) = '' then
    begin
      AErrorMsg := 'Indique la URL del servicio DeCA en la configuración.';
      Exit;
    end;

    LUrlPing := NormalizarUrlBase(AConfig.UrlServicio) + '/api/index.php?route=deca/ping&api_key=' + Trim(AConfig.ApiKey);
    try
      LHttp := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      LHttp.Open('GET', LUrlPing, False);
      LHttp.SetTimeouts(4000, 6000, 10000, 10000);
      LHttp.SetRequestHeader('X-API-Key', Trim(AConfig.ApiKey));
      LHttp.SetRequestHeader('Authorization', 'Bearer ' + Trim(AConfig.ApiKey));
      LHttp.Send();

      LStatus := LHttp.Status;
      LRespText := Trim(LHttp.ResponseText);

      if LStatus = 200 then
      begin
        Result := Pos('"success":true', LRespText) > 0;
        if Result then
          AInfoEmpresa := LRespText
        else
          AErrorMsg := 'Respuesta inesperada del servidor: ' + LRespText;
      end
      else if LStatus = 401 then
        AErrorMsg := 'No autorizado (401): Verifique la API Key de la empresa.'
      else
        AErrorMsg := Format('Error HTTP %d: %s'#13#10'%s', [LStatus, LHttp.StatusText, LRespText]);
    except
      on E: Exception do
        AErrorMsg := 'No se pudo conectar con el servidor: ' + E.Message;
    end;
  end
  else
  begin
    // Modo Local CLI
    LPhpExe := Trim(AConfig.RutaPhp);
    if (LPhpExe = '') or not FileExists(LPhpExe) then
    begin
      AErrorMsg := 'No se encontró el ejecutable PHP en: ' + LPhpExe;
      Exit;
    end;

    LScriptFile := Trim(AConfig.RutaScriptCli);
    if (LScriptFile = '') or not FileExists(LScriptFile) then
    begin
      AErrorMsg := 'No se encontró el script emisor en: ' + LScriptFile;
      Exit;
    end;

    Result := True;
    AInfoEmpresa := 'PHP y script CLI local disponibles correctamente.';
  end;
end;

function EjecutarCliLocal(const AConfig: TDecaConfig; const APayloadJson: string; out ARespJson: string): Boolean;
var
  LTempDir, LReqFile, LRespFile, LScriptFile, LPhpExe: string;
  LReqLines, LRespLines: TStringList;
  LStartupInfo: TStartupInfo;
  LProcessInfo: TProcessInformation;
  LSecAttr: TSecurityAttributes;
  LOutHandle, LInHandle: THandle;
  LCmdLine: string;
  LExitCode: DWORD;
  LCreatedOk: Boolean;
begin
  Result := False;
  ARespJson := '';
  LExitCode := 0;

  LTempDir := IncludeTrailingPathDelimiter(GetEnvironmentVariable('TEMP'));
  if (LTempDir = '') or not DirectoryExists(LTempDir) then
    LTempDir := ExtractFilePath(ParamStr(0));

  LReqFile := LTempDir + 'deca_req_' + IntToStr(GetTickCount) + '.json';
  LRespFile := LTempDir + 'deca_resp_' + IntToStr(GetTickCount) + '.json';
  LScriptFile := Trim(AConfig.RutaScriptCli);
  LPhpExe := Trim(AConfig.RutaPhp);

  if not FileExists(LPhpExe) then
  begin
    ARespJson := 'No se encontró el intérprete PHP en ' + LPhpExe;
    Exit;
  end;

  if not FileExists(LScriptFile) then
  begin
    ARespJson := 'No se encontró el script emisor en ' + LScriptFile;
    Exit;
  end;

  LReqLines := TStringList.Create;
  try
    LReqLines.Text := APayloadJson;
    LReqLines.SaveToFile(LReqFile, TEncoding.UTF8);
  finally
    LReqLines.Free;
  end;

  FillChar(LSecAttr, SizeOf(LSecAttr), 0);
  LSecAttr.nLength := SizeOf(SECURITY_ATTRIBUTES);
  LSecAttr.bInheritHandle := True;
  LSecAttr.lpSecurityDescriptor := nil;

  LOutHandle := CreateFile(PChar(LRespFile), GENERIC_WRITE,
    FILE_SHARE_READ or FILE_SHARE_WRITE, @LSecAttr, CREATE_ALWAYS,
    FILE_ATTRIBUTE_NORMAL, 0);

  if LOutHandle = INVALID_HANDLE_VALUE then
  begin
    ARespJson := 'No se pudo crear archivo de respuesta: ' + SysErrorMessage(GetLastError);
    DeleteFile(LReqFile);
    Exit;
  end;

  LInHandle := CreateFile('NUL', GENERIC_READ,
    FILE_SHARE_READ or FILE_SHARE_WRITE, @LSecAttr, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, 0);

  try
    FillChar(LStartupInfo, SizeOf(LStartupInfo), 0);
    LStartupInfo.cb := SizeOf(LStartupInfo);
    LStartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
    LStartupInfo.wShowWindow := SW_HIDE;
    LStartupInfo.hStdOutput := LOutHandle;
    LStartupInfo.hStdError := LOutHandle;
    LStartupInfo.hStdInput := LInHandle;

    LCmdLine := Format('"%s" "%s" "%s"', [LPhpExe, LScriptFile, LReqFile]);
    UniqueString(LCmdLine);

    LCreatedOk := CreateProcess(nil, PChar(LCmdLine), nil, nil, True, CREATE_NO_WINDOW, nil, nil, LStartupInfo, LProcessInfo);

    if LCreatedOk then
    begin
      WaitForSingleObject(LProcessInfo.hProcess, AConfig.TimeoutSeg * 1000);
      GetExitCodeProcess(LProcessInfo.hProcess, LExitCode);
      CloseHandle(LProcessInfo.hProcess);
      CloseHandle(LProcessInfo.hThread);
    end
    else
      ARespJson := 'Fallo al iniciar proceso PHP: ' + SysErrorMessage(GetLastError);
  finally
    CloseHandle(LOutHandle);
    if LInHandle <> INVALID_HANDLE_VALUE then
      CloseHandle(LInHandle);
  end;

  if LCreatedOk then
  begin
    if FileExists(LRespFile) then
    begin
      LRespLines := TStringList.Create;
      try
        LRespLines.LoadFromFile(LRespFile, TEncoding.UTF8);
        ARespJson := Trim(LRespLines.Text);
        Result := Pos('"success":true', ARespJson) > 0;
        if (not Result) and (ARespJson = '') then
          ARespJson := Format('El proceso PHP finalizó con código %d sin devolver datos.', [LExitCode]);
      finally
        LRespLines.Free;
        DeleteFile(LRespFile);
      end;
    end
    else
      ARespJson := Format('El proceso emisor no generó archivo de respuesta (código %d).', [LExitCode]);
  end
  else
    DeleteFile(LRespFile);

  DeleteFile(LReqFile);
end;

function InyectarApiKeyEnJson(const APayloadJson, AApiKey: string): string;
var
  LTrimmed, LKeyPart: string;
begin
  LTrimmed := Trim(APayloadJson);
  if (Trim(AApiKey) = '') or (LTrimmed = '') or (LTrimmed[1] <> '{') then
  begin
    Result := APayloadJson;
    Exit;
  end;

  // Inyectar "api_key":"..." tras la primera llave de apertura
  LKeyPart := '"api_key":"' + Trim(AApiKey) + '",';
  Result := '{' + LKeyPart + Copy(LTrimmed, 2, Length(LTrimmed) - 1);
end;

function EmitirDocumentoDeca(const AConfig: TDecaConfig; const APayloadJson: string; out ARespJson: string): Boolean;
var
  LUrlEmitir, LPayloadConKey: string;
begin
  if AConfig.Modo = dmRemotoApi then
  begin
    LUrlEmitir := NormalizarUrlBase(AConfig.UrlServicio) + '/api/index.php?route=deca/emitir&api_key=' + Trim(AConfig.ApiKey);
    LPayloadConKey := InyectarApiKeyEnJson(APayloadJson, AConfig.ApiKey);
    Result := EnviarHttpRest(LUrlEmitir, LPayloadConKey, AConfig.ApiKey, AConfig.TimeoutSeg, ARespJson);
    if Result then
      Result := Pos('"success":true', ARespJson) > 0;
  end
  else
  begin
    LPayloadConKey := InyectarApiKeyEnJson(APayloadJson, AConfig.ApiKey);
    Result := EjecutarCliLocal(AConfig, LPayloadConKey, ARespJson);
  end;
end;

function ConsultarDocumentoDeca(const AConfig: TDecaConfig; const ACodigo, AReferencia: string; out ARespJson: string): Boolean;
var
  LUrl, LParamCod, LParamRef: string;
  LHttp: OleVariant;
  LStatus: Integer;
begin
  Result := False;
  ARespJson := '';

  if (Trim(ACodigo) = '') and (Trim(AReferencia) = '') then Exit;

  if AConfig.Modo = dmRemotoApi then
  begin
    LParamCod := StringReplace(Trim(ACodigo), ' ', '%20', [rfReplaceAll]);
    LParamRef := StringReplace(Trim(AReferencia), ' ', '%20', [rfReplaceAll]);
    LParamRef := StringReplace(LParamRef, '/', '%2F', [rfReplaceAll]);

    LUrl := NormalizarUrlBase(AConfig.UrlServicio) + '/api/index.php?route=deca/consultar&api_key=' + Trim(AConfig.ApiKey);
    if LParamCod <> '' then
      LUrl := LUrl + '&codigo=' + LParamCod;
    if LParamRef <> '' then
      LUrl := LUrl + '&referencia=' + LParamRef;

    try
      LHttp := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      LHttp.Open('GET', LUrl, False);
      LHttp.SetTimeouts(4000, 6000, AConfig.TimeoutSeg * 1000, AConfig.TimeoutSeg * 1000);
      if Trim(AConfig.ApiKey) <> '' then
      begin
        LHttp.SetRequestHeader('X-API-Key', Trim(AConfig.ApiKey));
        LHttp.SetRequestHeader('Authorization', 'Bearer ' + Trim(AConfig.ApiKey));
      end;
      LHttp.Send();

      LStatus := LHttp.Status;
      ARespJson := Trim(LHttp.ResponseText);

      Result := (LStatus = 200) and (Pos('"success":true', ARespJson) > 0) and (Pos('"encontrado":true', ARespJson) > 0);
    except
      on E: Exception do
        ARespJson := 'Error de comunicación HTTPS: ' + E.Message;
    end;
  end;
end;

function ObtenerPresetsVehiculos(const AConfig: TDecaConfig; out ARespJson: string): Boolean;
var
  LUrl: string;
  LHttp: OleVariant;
  LStatus: Integer;
begin
  Result := False;
  ARespJson := '';
  LUrl := NormalizarUrlBase(AConfig.UrlServicio) + '/api/index.php?route=presets/vehiculos&api_key=' + Trim(AConfig.ApiKey);
  try
    LHttp := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    LHttp.Open('GET', LUrl, False);
    LHttp.SetTimeouts(4000, 6000, AConfig.TimeoutSeg * 1000, AConfig.TimeoutSeg * 1000);
    if Trim(AConfig.ApiKey) <> '' then
    begin
      LHttp.SetRequestHeader('X-API-Key', Trim(AConfig.ApiKey));
      LHttp.SetRequestHeader('Authorization', 'Bearer ' + Trim(AConfig.ApiKey));
    end;
    LHttp.Send();
    LStatus := LHttp.Status;
    ARespJson := Trim(LHttp.ResponseText);
    Result := (LStatus = 200) and (Pos('"success":true', ARespJson) > 0);
  except
    on E: Exception do
      ARespJson := 'Error: ' + E.Message;
  end;
end;

function ObtenerPresetsContactos(const AConfig: TDecaConfig; const ATipo: string; out ARespJson: string): Boolean;
var
  LUrl: string;
  LHttp: OleVariant;
  LStatus: Integer;
begin
  Result := False;
  ARespJson := '';
  LUrl := NormalizarUrlBase(AConfig.UrlServicio) + '/api/index.php?route=presets/contactos&api_key=' + Trim(AConfig.ApiKey);
  if Trim(ATipo) <> '' then
    LUrl := LUrl + '&tipo=' + Trim(ATipo);
  try
    LHttp := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    LHttp.Open('GET', LUrl, False);
    LHttp.SetTimeouts(4000, 6000, AConfig.TimeoutSeg * 1000, AConfig.TimeoutSeg * 1000);
    if Trim(AConfig.ApiKey) <> '' then
    begin
      LHttp.SetRequestHeader('X-API-Key', Trim(AConfig.ApiKey));
      LHttp.SetRequestHeader('Authorization', 'Bearer ' + Trim(AConfig.ApiKey));
    end;
    LHttp.Send();
    LStatus := LHttp.Status;
    ARespJson := Trim(LHttp.ResponseText);
    Result := (LStatus = 200) and (Pos('"success":true', ARespJson) > 0);
  except
    on E: Exception do
      ARespJson := 'Error: ' + E.Message;
  end;
end;

function GuardarPresetVehiculo(const AConfig: TDecaConfig; const APayloadJson: string): Boolean;
var
  LUrl, LResp: string;
begin
  LUrl := NormalizarUrlBase(AConfig.UrlServicio) + '/api/index.php?route=presets/vehiculos&api_key=' + Trim(AConfig.ApiKey);
  Result := EnviarHttpRest(LUrl, APayloadJson, AConfig.ApiKey, AConfig.TimeoutSeg, LResp);
end;

function GuardarPresetContacto(const AConfig: TDecaConfig; const APayloadJson: string): Boolean;
var
  LUrl, LResp: string;
begin
  LUrl := NormalizarUrlBase(AConfig.UrlServicio) + '/api/index.php?route=presets/contactos&api_key=' + Trim(AConfig.ApiKey);
  Result := EnviarHttpRest(LUrl, APayloadJson, AConfig.ApiKey, AConfig.TimeoutSeg, LResp);
end;

end.
