CREATE TABLE IF NOT EXISTS produtos (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, empresa_id BIGINT UNSIGNED NOT NULL, codigo VARCHAR(20) NOT NULL,
 tipo VARCHAR(20) NOT NULL DEFAULT 'mercadoria', descricao VARCHAR(200) NOT NULL, descricao_reduzida VARCHAR(80), referencia VARCHAR(60), gtin VARCHAR(14),
 marca_id BIGINT UNSIGNED, grupo_id BIGINT UNSIGNED, subgrupo_id BIGINT UNSIGNED, secao_id BIGINT UNSIGNED, linha_id BIGINT UNSIGNED, unidade_id BIGINT UNSIGNED NOT NULL,
 custo_referencia DECIMAL(15,4) NOT NULL DEFAULT 0, margem_sugerida DECIMAL(9,4) NOT NULL DEFAULT 0, desconto_maximo DECIMAL(9,4) NOT NULL DEFAULT 0, comissao_padrao DECIMAL(9,4) NOT NULL DEFAULT 0,
 quantidade_minima_venda DECIMAL(15,6) NOT NULL DEFAULT 1, multiplo_venda DECIMAL(15,6) NOT NULL DEFAULT 1, venda_fracionada BOOLEAN NOT NULL DEFAULT FALSE,
 controla_estoque BOOLEAN NOT NULL DEFAULT TRUE, permite_estoque_negativo BOOLEAN NOT NULL DEFAULT FALSE, controla_lote BOOLEAN NOT NULL DEFAULT FALSE,
 controla_serie BOOLEAN NOT NULL DEFAULT FALSE, controla_validade BOOLEAN NOT NULL DEFAULT FALSE, estoque_minimo DECIMAL(15,6) NOT NULL DEFAULT 0, estoque_maximo DECIMAL(15,6) NOT NULL DEFAULT 0,
 ncm VARCHAR(8), cest VARCHAR(7), origem_mercadoria TINYINT NOT NULL DEFAULT 0, gtin_tributavel VARCHAR(14), unidade_tributavel VARCHAR(10), beneficio_fiscal VARCHAR(20),
 peso_liquido DECIMAL(15,6) NOT NULL DEFAULT 0, peso_bruto DECIMAL(15,6) NOT NULL DEFAULT 0, altura DECIMAL(15,4) NOT NULL DEFAULT 0, largura DECIMAL(15,4) NOT NULL DEFAULT 0, comprimento DECIMAL(15,4) NOT NULL DEFAULT 0,
 observacoes TEXT, ativo BOOLEAN NOT NULL DEFAULT TRUE, criado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, atualizado_em TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, excluido_em TIMESTAMP NULL,
 FOREIGN KEY(empresa_id) REFERENCES empresas(id), FOREIGN KEY(marca_id) REFERENCES marcas(id), FOREIGN KEY(grupo_id) REFERENCES grupos_produto(id), FOREIGN KEY(subgrupo_id) REFERENCES subgrupos_produto(id), FOREIGN KEY(secao_id) REFERENCES secoes_produto(id), FOREIGN KEY(linha_id) REFERENCES linhas_produto(id), FOREIGN KEY(unidade_id) REFERENCES unidades_produto(id),
 INDEX idx_produtos_empresa_codigo(empresa_id,codigo), INDEX idx_produtos_empresa_descricao(empresa_id,descricao)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
