SET NAMES utf8mb4;

INSERT INTO automation_connectors
(
    code,
    name,
    description,
    base_url,
    icon_class,
    sort_order,
    status,
    notes
)
SELECT
    'SAGA',
    'SAGA WEB',
    'Contabilitate, rapoarte și arhivă',
    'https://web.sagasoft.ro/',
    'bi-calculator',
    10,
    'active',
    'Autentificare prin Chrome persistent, fără Node.js.'
WHERE NOT EXISTS (
    SELECT 1
    FROM automation_connectors
    WHERE code='SAGA'
);

INSERT INTO automation_recipes
(
    connector_id,
    code,
    name,
    category,
    description,
    recipe_json,
    output_extension,
    sort_order,
    status
)
SELECT
    c.id,
    'SAGA_LOGIN',
    'Autentificare SAGA WEB',
    'Autentificare',
    'Deschide Chrome pentru login manual și cod e-mail.',
    CONCAT(
        '{"title":"Autentificare SAGA WEB","connectorId":',
        c.id,
        ',"steps":[',
        '{"type":"manual","message":"În Chrome: autentifică-te în SAGA WEB, introdu codul primit pe e-mail, așteaptă pagina principală, apoi revino și apasă OK."},',
        '{"type":"wait","ms":1500}',
        ']}'
    ),
    '',
    1,
    'active'
FROM automation_connectors c
WHERE c.code='SAGA'
  AND NOT EXISTS (
      SELECT 1
      FROM automation_recipes r
      WHERE r.connector_id=c.id
        AND r.code='SAGA_LOGIN'
  );
