-- Migration 23: Sync, telemetria e observabilidade
-- Source: backend/db/dump/19052026_nextbar.sql

-- -------------------------------------------------------- 23. Sync, telemetria e observabilidade
--
-- Estrutura para tabela `eventos_sync`
--

CREATE TABLE `eventos_sync` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  device_id INT NOT NULL,
  event_id INT NULL,
  sync_type VARCHAR(100) NOT NULL,
  idempotency_key VARCHAR(200) NOT NULL,
  payload_json JSON NOT NULL,
  error TEXT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  processed_at TIMESTAMP NULL,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (tenant_id) REFERENCES tenants(id),
  FOREIGN KEY (device_id) REFERENCES dispositivos(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `heartbeats_dispositivo`
--

CREATE TABLE `heartbeats_dispositivo` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  device_id INT NOT NULL,
  tenant_id INT NOT NULL,
  event_id INT NULL,
  sector_id INT NULL,
  operator_user_id INT NULL,
  app_version VARCHAR(100) NOT NULL,
  online TINYINT(1) NOT NULL DEFAULT 1,
  pending_sales_count INT(11) NOT NULL DEFAULT 0,
  pending_sync_events_count INT(11) NOT NULL DEFAULT 0,
  battery_level INT(11) NULL,
  network_type VARCHAR(100) NULL,
  last_sale_at TIMESTAMP NULL,
  last_sync_at TIMESTAMP NULL,
  sent_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  received_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (device_id) REFERENCES dispositivos(id),
  FOREIGN KEY (tenant_id) REFERENCES tenants(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id),
  FOREIGN KEY (sector_id) REFERENCES setores(id),
  FOREIGN KEY (operator_user_id) REFERENCES usuarios(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `logs_erro_dispositivo`
--

CREATE TABLE `logs_erro_dispositivo` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  device_id INT NOT NULL,
  event_id INT NULL,
  severity VARCHAR(50) NOT NULL,
  error_type VARCHAR(100) NOT NULL,
  message TEXT NOT NULL,
  stack_trace TEXT NULL,
  payload_json JSON NULL,
  created_at TIMESTAMP NOT NULL DEFAULT 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 (device_id) REFERENCES dispositivos(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `incidentes`
--

CREATE TABLE `incidentes` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  event_id INT NOT NULL,
  device_id INT NULL,
  sector_id INT NULL,
  severity VARCHAR(50) NOT NULL,
  title VARCHAR(200) NOT NULL,
  description TEXT NOT NULL,
  started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  resolved_at TIMESTAMP NULL,
  responsible_user_id INT NULL,
  root_cause TEXT NULL,
  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 (device_id) REFERENCES dispositivos(id),
  FOREIGN KEY (sector_id) REFERENCES setores(id),
  FOREIGN KEY (responsible_user_id) REFERENCES usuarios(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `acoes_incidente`
--

CREATE TABLE `acoes_incidente` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  incident_id INT NOT NULL,
  action_type VARCHAR(100) NOT NULL,
  description TEXT NOT NULL,
  created_by INT NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (incident_id) REFERENCES incidentes(id),
  FOREIGN KEY (created_by) REFERENCES usuarios(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
