-- Orçamentos de venda (quotes)

CREATE TABLE IF NOT EXISTS sales_quotes (
  id INT AUTO_INCREMENT PRIMARY KEY,
  numero VARCHAR(40) NOT NULL UNIQUE,
  cliente_id INT NOT NULL,
  valor_total DECIMAL(12,2) NOT NULL DEFAULT 0,
  status VARCHAR(20) NOT NULL DEFAULT 'rascunho', -- rascunho, aprovado, expirado, cancelado
  validade DATE NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  INDEX idx_sales_quotes_cliente (cliente_id),
  INDEX idx_sales_quotes_status (status),
  INDEX idx_sales_quotes_created (created_at)
);

CREATE TABLE IF NOT EXISTS sales_quote_items (
  id INT AUTO_INCREMENT PRIMARY KEY,
  quote_id INT NOT NULL,
  product_id INT NOT NULL,
  quantidade DECIMAL(12,3) NOT NULL,
  preco DECIMAL(12,2) NOT NULL,
  total DECIMAL(12,2) NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_sq_items_quote (quote_id),
  INDEX idx_sq_items_product (product_id)
);

CREATE INDEX IF NOT EXISTS idx_sq_numero ON sales_quotes (numero);