/* ICYLAB Billing v7.2.2 — pie configurable para documentos comerciales.
 * Idempotente. No define contenido inicial: NULL/vacío significa sin pie.
 */

SET @has_commercial_document_footer := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'system_settings'
      AND column_name = 'commercial_document_footer'
);

SET @footer_ddl := IF(
    @has_commercial_document_footer = 0,
    'ALTER TABLE system_settings ADD COLUMN commercial_document_footer TEXT NULL AFTER bank_email',
    'SELECT 1'
);

PREPARE stmt_footer_ddl FROM @footer_ddl;
EXECUTE stmt_footer_ddl;
DEALLOCATE PREPARE stmt_footer_ddl;

SELECT
    DATABASE() AS active_database,
    COUNT(*) AS commercial_document_footer_columns,
    CASE WHEN COUNT(*) = 1 THEN 'READY: SQL V7.2.2 APPLIED' ELSE 'BLOCKED: COLUMN NOT AVAILABLE' END AS decision
FROM information_schema.columns
WHERE table_schema = DATABASE()
  AND table_name = 'system_settings'
  AND column_name = 'commercial_document_footer';
