/* ICYLAB Billing v7.0.0 - habilitación controlada y auditoría de acciones masivas.
   Ejecutar una vez, después de respaldar. No altera documentos, cobros ni pagos históricos. */

START TRANSACTION;

UPDATE billing_document_types
SET is_active=1, enabled_production=1, updated_at=CURRENT_TIMESTAMP
WHERE direction='issued'
  AND document_key IN (
    'invoice_electronic','invoice_exempt_electronic',
    'receipt_electronic','receipt_exempt_electronic',
    'invoice_export_electronic','dispatch_guide_electronic',
    'credit_note_electronic','debit_note_electronic',
    'credit_note_export_electronic','debit_note_export_electronic'
  );

CREATE TABLE IF NOT EXISTS billing_bulk_action_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  environment ENUM('sandbox','production') NOT NULL,
  scope VARCHAR(40) NOT NULL,
  action_key VARCHAR(60) NOT NULL,
  selected_count INT UNSIGNED NOT NULL DEFAULT 0,
  filters_json LONGTEXT NULL,
  result_message VARCHAR(500) NULL,
  created_by INT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_bulk_action_lookup (environment,scope,action_key,created_at),
  KEY idx_bulk_action_user (created_by,created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

COMMIT;

SELECT document_key, enabled_sandbox, enabled_production, is_active
FROM billing_document_types
WHERE direction='issued'
ORDER BY facto_document_type_id;
