-- Migration 14: Pagamentos, estornos e chargebacks
-- Source: backend/db/dump/19052026_nextbar.sql

-- -------------------------------------------------------- 14. Pagamentos, estornos e chargebacks
--
-- Estrutura para tabela `tentativas_pagamento`
--

CREATE TABLE `tentativas_sincronizacao_vendas` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  sale_id INT NOT NULL,
  gateway VARCHAR(200) NOT NULL,
  method VARCHAR(200) NOT NULL,
  sale_price DECIMAL(10,2) NOT NULL,
  gateway_transaction_id VARCHAR(200),
  authorization_code VARCHAR(200),
  nsu VARCHAR(200),
  installments INT(11),
  erro_code VARCHAR(200),
  error_message TEXT,
  tempted_in TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  confirmed_in TIMESTAMP NULL,
  raw_json_response TEXT,
  attempt_number INT(11) NOT NULL,
  success TINYINT(1) NOT NULL,
  attempted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (sale_id) REFERENCES vendas(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `estornos`
--

CREATE TABLE `estornos`(
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  sale_id INT NOT NULL,
  tenant_id INT NOT NULL,
  event_id INT NOT NULL,
  sector_id INT NOT NULL,
  payment_id INT NOT NULL,
  sale VARCHAR(200) NOT NULL,
  motivation VARCHAR(200) NOT NULL,
  request_by INT(11) NOT NULL,
  processed_by INT(11),
  request_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  processed_at TIMESTAMP NULL,
  gateway_refund VARCHAR(200),
  observations TEXT,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (tenant_id) REFERENCES tenants(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id),
  FOREIGN KEY (sector_id) REFERENCES setores(id),
  FOREIGN KEY (sale_id) REFERENCES vendas(id),
  FOREIGN KEY (request_by) REFERENCES usuarios(id),
  FOREIGN KEY (processed_by) REFERENCES usuarios(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
