-- Emissões de NFC-e (mock homologação RJ inicialmente)
CREATE TABLE IF NOT EXISTS nfce_emissions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  sale_id INT NULL,
  status VARCHAR(20) NOT NULL DEFAULT 'authorized', -- authorized, canceled, error
  chave VARCHAR(44) NOT NULL,
  protocolo VARCHAR(64) NULL,
  xml MEDIUMTEXT NULL,
  environment VARCHAR(10) NOT NULL DEFAULT 'homolog', -- homolog | prod
  messages TEXT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_nfce_sale (sale_id),
  UNIQUE KEY uq_nfce_chave (chave)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;