#!/usr/bin/env bash
set -euo pipefail

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products.txt

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products.jsonl \
  --format jsonl

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products.md \
  --format md

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products.xml \
  --format xml

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products.feather \
  --format feather

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT category, SUM(stock) AS total_stock FROM table GROUP BY category ORDER BY total_stock DESC" \
  --output ./examples/stock-by-category.xlsx \
  --format xlsx

cargo run -- \
  query \
  --input ./examples/inventory.xml:Inventory \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products-from-xml.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.csv \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products-from-csv.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.jsonl \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products-from-jsonl.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.json:Inventory \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products-from-json.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.md \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products-from-markdown.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.html \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products-from-html.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.feather \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products-from-feather.csv \
  --format csv

cargo run -- \
  query \
  --input ./examples/inventory.xlsx \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output ./examples/active-products.html \
  --format html

cargo run -- \
  query \
  --input '@clipboard|csv' \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC" \
  --output '@clipboard|json' \
  --format json

cargo run -- \
  diff \
  --key id \
  --ignore-columns updated_at \
  ./examples/snapshot-before.csv \
  ./examples/snapshot-after.csv \
  --output ./examples/diff-products.txt \
  --format text

cargo run -- \
  diff \
  --key id \
  --ignore-columns updated_at \
  --show all \
  --side-by-side \
  ./examples/snapshot-before.csv \
  ./examples/snapshot-after.csv \
  --output ./examples/diff-products-side-by-side.md \
  --format md

cargo run -- \
  diff \
  --schema-only \
  ./examples/snapshot-before.csv \
  ./examples/snapshot-after.csv \
  --output ./examples/diff-schema.json \
  --format json

# CTE chaining with --with
cargo run -- \
  query \
  --input ./examples/inventory.csv \
  --with 'active AS (SELECT * FROM table WHERE active = 1)' \
  --with 'pricey AS (SELECT * FROM active WHERE price > 5)' \
  --sql 'SELECT category, COUNT(*) AS cnt FROM pricey GROUP BY category ORDER BY cnt DESC' \
  --output ./examples/cte-chaining-example.csv \
  --format csv

# Multi-statement SQL file
cargo run -- \
  query \
  --input ./examples/inventory.csv \
  --sql-file ./examples/multi-statement.sql \
  --output ./examples/multi-statement-result.csv \
  --format csv

# AI capabilities discovery (JSON)
cargo run -- capabilities

# AI mode envelope (JSON)
cargo run -- \
  --ai-mode \
  query \
  --input ./examples/inventory.csv \
  --sql "SELECT product, price FROM table WHERE active = 1 ORDER BY price DESC"

# AI mode diff envelope
cargo run -- \
  --ai-mode \
  diff \
  --key id \
  ./examples/snapshot-before.csv \
  ./examples/snapshot-after.csv

# AI mode pivot envelope
cargo run -- \
  --ai-mode \
  pivot \
  --input ./examples/inventory.csv \
  --query 'PIVOT SUM(stock) FOR active FROM table GROUP BY category'

# AI mode conversion envelope
cargo run -- \
  --ai-mode \
  conv \
  --input ./examples/inventory.csv \
  --format json
