-- RM Consultas v9: correções pontuais do fluxo CRLV-e
-- Baseado na documentação oficial UltraBusca v2.0 (2026):
-- CRLV-e documentados: AL, AP, MA, MS, PI, PR, RO, RR, SE, TO, MT, GO, MG, SP.
-- Todos usam placa + token no endpoint /searchapiss/api/consultar.php.

ALTER TABLE consultas ADD INDEX idx_consulta_cache (api_id, identifier, id);

-- Desativa APIs de CRLV-e que não aparecem na documentação pública atual.
UPDATE consultas_apis
SET ativo=0
WHERE nome_fornecedor IN (
 'CRLV-e AC','CRLV-e AM','CRLV-e BA','CRLV-e CE','CRLV-e DF',
 'CRLV-e PB','CRLV-e PE','CRLV-e RJ','CRLV-e RN','CRLV-e RS','CRLV-e SC'
);

-- Garante as 14 APIs de CRLV-e documentadas.
INSERT INTO consultas_apis(nome,nome_fornecedor,custo,preco,ativo) VALUES
('CRLV-e AL','CRLV-e AL',0,39.90,1),
('CRLV-e AP','CRLV-e AP',0,39.90,1),
('CRLV-e MA','CRLV-e MA',0,39.90,1),
('CRLV-e MS','CRLV-e MS',0,39.90,1),
('CRLV-e PI','CRLV-e PI',0,39.90,1),
('CRLV-e PR','CRLV-e PR',0,39.90,1),
('CRLV-e RO','CRLV-e RO',0,39.90,1),
('CRLV-e RR','CRLV-e RR',0,39.90,1),
('CRLV-e SE','CRLV-e SE',0,39.90,1),
('CRLV-e TO','CRLV-e TO',0,39.90,1),
('CRLV-e MT','CRLV-e MT',0,39.90,1),
('CRLV-e GO','CRLV-e GO',0,39.90,1),
('CRLV-e MG','CRLV-e MG',0,39.90,1),
('CRLV-e SP','CRLV-e SP',0,39.90,1)
ON DUPLICATE KEY UPDATE ativo=1;
