-- Credenciales de la API REST de SUNAT (GRE) para abisfact.
-- El client_id y client_secret se obtienen al registrar la empresa en el
-- portal de clientes SOL de SUNAT para consumir api-cpe.sunat.gob.pe.

SET @client_id_exists := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'empresas'
      AND column_name = 'sunat_client_id'
);

SET @sql_client_id := IF(
    @client_id_exists = 0,
    'ALTER TABLE empresas ADD COLUMN sunat_client_id VARCHAR(120) NULL AFTER sunat_clave_secundaria',
    'SELECT 1'
);
PREPARE stmt_client_id FROM @sql_client_id;
EXECUTE stmt_client_id;
DEALLOCATE PREPARE stmt_client_id;

SET @client_secret_exists := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'empresas'
      AND column_name = 'sunat_client_secret'
);

SET @sql_client_secret := IF(
    @client_secret_exists = 0,
    'ALTER TABLE empresas ADD COLUMN sunat_client_secret VARCHAR(255) NULL AFTER sunat_client_id',
    'SELECT 1'
);
PREPARE stmt_client_secret FROM @sql_client_secret;
EXECUTE stmt_client_secret;
DEALLOCATE PREPARE stmt_client_secret;