-- Migration 11: Estoque
-- Source: backend/db/dump/19052026_nextbar.sql

-- -------------------------------------------------------- 11. Estoque
--
-- Estrutura para tabela `saldo_estoque`
--

CREATE TABLE `saldo_estoque`(
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  event_id INT NOT NULL,
  sector_id INT NOT NULL,
  product_tenant_id INT NOT NULL,
  available_qtd INT(11) NOT NULL,
  reserved_qtd INT(11) NOT NULL,
  qtd_sold INT(11) NOT NULL,
  min_qtd INT(11) NOT NULL,
  max_qtd INT(11) NOT 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 (product_tenant_id) REFERENCES produtos_tenant(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `movimentos_estoque`
--

CREATE TABLE `movimentos_estoque` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  event_id INT NOT NULL,
  sector_id INT NOT NULL,
  product_tenant_id INT NOT NULL,
  qtd_change INT(11) NOT NULL,
  qtd_before INT(11) NOT NULL,
  qtd_after INT(11) NOT NULL,
  sale_id INT(11),
  transfer_id INT(11),
  description TEXT,
  created_by INT(11),
  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 (product_tenant_id) REFERENCES produtos_tenant(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `alocacoes_estoque_dispositivo`
--

CREATE TABLE `alocacoes_estoque_dispositivo` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  event_id INT NOT NULL,
  sector_id INT NOT NULL,
  product_tenant_id INT NOT NULL,
  device_id INT NOT NULL,
  allocated_qtd INT(11) NOT NULL,
  quantity_sold INT(11),
  remaining_quantity INT(11),
  created_by INT(11),
  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 (product_tenant_id) REFERENCES produtos_tenant(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id),
  FOREIGN KEY (device_id) REFERENCES dispositivos(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
