*****************************************************************************************
* test_autopass_driver.prg
* Driver .prg fara forma pentru US-003 (client AutopassClient) - PRD ROAAUTO-1.0,
* sectiunea "Acoperire de test": "driver .prg fara forma (1 comanda hand-built) - E2E:
* acopera encoding/headers/timeouts izolat".
*
* Ruleaza autonom din Command Window:
*   DO Programe\test_autopass_driver.prg
*
* Ce face:
*   (a) apeleaza autopass_test_conexiune() si afiseaza rezultatul (ping)
*   (b) construieste MANUAL (fara Oracle) 2 obiecte comanda in formatul asteptat de
*       AutopassClient.EnviaLot - una din ele cu operatii cu diacritice in denumire,
*       ca sa acopere codepath-ul de encoding
*   (c) apeleaza Valideaza (dry-run) pe cele 2 comenzi
*   (d) doar cu confirmare MESSAGEBOX explicita SI doar daca mediul citit din config e
*       "test", apeleaza si TrimiteLot - altfel se opreste dupa dry-run
*   (e) afiseaza raspunsurile per element (index, id_ordl, status, motiv, submission_id)
*
* Nu se apeleaza din meniul aplicatiei - e un instrument de diagnostic, lansat manual.
*****************************************************************************************

CLOSE ALL
CLEAR

SET PROCEDURE TO Programe\oproceduri_autopass.prg ADDITIVE
SET PROCEDURE TO COMUN\utile\nfjson\nfjsoncreate.prg ADDITIVE
SET PROCEDURE TO COMUN\utile\nfjson\nfjsonread.prg ADDITIVE

LOCAL loPing, oClient, loComenzi, loRezValidare, loRezTrimite, loConfig
LOCAL lnAlegere, i, loItem

?'============================================================================='
?'AUTOPASS - driver de test fara forma'
?'============================================================================='

*-- (a) test conexiune (GET /v1/ping) -------------------------------------------------
?
?'--- (a) autopass_test_conexiune() ---'

loPing = autopass_test_conexiune()

?'  lSucces               : ' + TRANSFORM(loPing.lSucces)
?'  cMediu                : ' + loPing.cMediu
?'  lAutentificatCuCheie  : ' + TRANSFORM(loPing.lAutentificatCuCheie)
?'  lAreCredsTest         : ' + TRANSFORM(loPing.lAreCredsTest)
?'  lAreCredsProd         : ' + TRANSFORM(loPing.lAreCredsProd)
?'  cEroare               : ' + loPing.cEroare

IF !loPing.lSucces
	?
	?'Ping esuat - verifica AUTOPASS_URL/AUTOPASS_KEY in tabelul optiuni inainte de a continua.'
	?'Driverul continua totusi (poate testa doar constructia payload-ului si Valideaza va esua controlat).'
ENDIF

*-- (b) construim manual 2 "comenzi", fara Oracle --------------------------------------
?
?'--- (b) construire manuala a comenzilor de test ---'

loComenzi = Createobject('Collection')

*-- comanda 1: fara diacritice, caz de baza
loItem = Createobject('empty')
AddProperty(loItem, 'id_ordl', 900001)
AddProperty(loItem, 'vin', 'WVWZZZ1KZAW000001')
AddProperty(loItem, 'nrinmat', 'B99TST01')
AddProperty(loItem, 'data_prestatie', '2026-07-01')
AddProperty(loItem, 'odometru_final', '123456')

LOCAL ARRAY laPrestatii1(2)
laPrestatii1(1) = Createobject('empty')
AddProperty(laPrestatii1(1), 'cod_op_service', 'SCH001')
AddProperty(laPrestatii1(1), 'denumire', 'Schimb ulei motor')
laPrestatii1(2) = Createobject('empty')
AddProperty(laPrestatii1(2), 'cod_op_service', 'FRN010')
AddProperty(laPrestatii1(2), 'denumire', 'Verificare frane')
AddProperty(loItem, 'prestatii(2)')
Acopy(laPrestatii1, loItem.prestatii)

AddProperty(loItem, 'obs', autopass_obs(90001, 190001))

loComenzi.Add(loItem)

*-- comanda 2: cu diacritice in denumirea operatiei - acopera codepath-ul de encoding
loItem = Createobject('empty')
AddProperty(loItem, 'id_ordl', 900002)
AddProperty(loItem, 'vin', 'WVWZZZ1KZAW000002')
AddProperty(loItem, 'nrinmat', 'B99TST02')
AddProperty(loItem, 'data_prestatie', '2026-07-02')
AddProperty(loItem, 'odometru_final', '87654')

LOCAL ARRAY laPrestatii2(2)
laPrestatii2(1) = Createobject('empty')
AddProperty(laPrestatii2(1), 'cod_op_service', 'DIAC01')
* diacritice romanesti in text sursa (comentariile raman fara diacritice, dar
* denumirea trimisa in payload TREBUIE sa contina diacriticele reale, ca sa testeze
* efectiv encoding-ul):
AddProperty(laPrestatii2(1), 'denumire', 'īnlocuire ambreiaj - verificare etanās la strāngere')
laPrestatii2(2) = Createobject('empty')
AddProperty(laPrestatii2(2), 'cod_op_service', 'DIAC02')
AddProperty(laPrestatii2(2), 'denumire', 'Reglaj geometrie roīi faāa/spate')
AddProperty(loItem, 'prestatii(2)')
Acopy(laPrestatii2, loItem.prestatii)

AddProperty(loItem, 'obs', autopass_obs(90002, 190002))

loComenzi.Add(loItem)

?'  Comenzi construite: ' + TRANSFORM(loComenzi.Count)
FOR i = 1 TO loComenzi.Count
	loItem = loComenzi.Item(i)
	?'    [' + TRANSFORM(i) + '] id_ordl=' + TRANSFORM(loItem.id_ordl) + ;
		' vin=' + loItem.vin + ' obs="' + loItem.obs + '"'
ENDFOR

*-- (c) Valideaza (dry-run) -------------------------------------------------------------
?
?'--- (c) AutopassClient.Valideaza() [dry-run, fara enqueue] ---'

oClient = Createobject('AutopassClient')

loRezValidare = .NULL.
TRY
	loRezValidare = oClient.Valideaza(loComenzi)
CATCH TO loEx
	?'  EROARE la Valideaza(): ' + loEx.Message
ENDTRY

IF VARTYPE(loRezValidare) = 'O'
	?'  lSucces      : ' + TRANSFORM(loRezValidare.lSucces)
	?'  nStatusHttp  : ' + TRANSFORM(loRezValidare.nStatusHttp)
	?'  cDetaliu     : ' + loRezValidare.cDetaliu

	IF loRezValidare.lSucces AND VARTYPE(loRezValidare.oRezultate) = 'O'
		?'  Rezultate dry-run:'
		FOR EACH loItem IN loRezValidare.oRezultate
			?'    index=' + TRANSFORM(loItem.index) + ;
				' id_ordl=' + TRANSFORM(loItem.id_ordl) + ;
				' valid=' + TRANSFORM(loItem.valid) + ;
				' status_estimat=' + TRANSFORM(EVL(loItem.status_estimat,'')) + ;
				' nemapate=' + IIF(TYPE('loItem.nemapate') = 'A', TRANSFORM(ALEN(loItem.nemapate,1)), '0')
		ENDFOR
	ENDIF
ENDIF

*-- (d) TrimiteLot - DOAR cu confirmare explicita SI DOAR daca mediul e test -------------
?
?'--- (d) AutopassClient.TrimiteLot() [enqueue real - necesita confirmare] ---'

loConfig = autopass_get_config()

IF UPPER(ALLTRIM(loConfig.cRarEnv)) <> 'TEST'
	?'  Mediul curent din configurare (AUTOPASS_ENV) nu este "test" (este: "' + ;
		loConfig.cRarEnv + '") - TrimiteLot NU se apeleaza din driver.'
	?'  Seteaza AUTOPASS_ENV=test in tabelul optiuni daca vrei sa testezi enqueue-ul real.'
ELSE
	lnAlegere = MESSAGEBOX('Mediul este TEST. Trimiti (enqueue real) cele ' + ;
		TRANSFORM(loComenzi.Count) + ' comenzi de test la gateway-ul AUTOPASS?' + CHR(13) + ;
		'Aceasta e o transmitere reala pe mediul de test (nu Productie).', 4 + 32, 'AUTOPASS - confirmare TrimiteLot')

	IF lnAlegere = 6  && Yes
		loRezTrimite = .NULL.
		TRY
			loRezTrimite = oClient.TrimiteLot(loComenzi)
		CATCH TO loEx
			?'  EROARE la TrimiteLot(): ' + loEx.Message
		ENDTRY

		IF VARTYPE(loRezTrimite) = 'O'
			?'  lSucces      : ' + TRANSFORM(loRezTrimite.lSucces)
			?'  nStatusHttp  : ' + TRANSFORM(loRezTrimite.nStatusHttp)
			?'  cDetaliu     : ' + loRezTrimite.cDetaliu

			IF loRezTrimite.lSucces AND VARTYPE(loRezTrimite.oRezultate) = 'O'
				?'  Rezultate TrimiteLot:'
				FOR EACH loItem IN loRezTrimite.oRezultate
					?'    index=' + TRANSFORM(loItem.index) + ;
						' id_ordl=' + TRANSFORM(loItem.id_ordl) + ;
						' status=' + TRANSFORM(loItem.status) + ;
						' motiv=' + TRANSFORM(loItem.motiv) + ;
						' submission_id=' + TRANSFORM(loItem.submission_id) + ;
						' id_prezentare=' + TRANSFORM(loItem.id_prezentare)
				ENDFOR
			ENDIF
		ENDIF
	ELSE
		?'  Renuntat de utilizator - TrimiteLot nu s-a apelat.'
	ENDIF
ENDIF

?
?'============================================================================='
?'Driver terminat.'
?'============================================================================='

RETURN
