#!/bin/bash

# Demo script for Wasot CSS-like selectors
# Shows all implemented selector types with examples

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
BOLD='\033[1m'

# Demo file
DEMO_FILE="examples/selectors_demo.wasot"

# Function to run a selector and display results
run_selector() {
    local selector="$1"
    local description="$2"
    
    echo -e "\n${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${BOLD}${BLUE}Selector:${NC} ${YELLOW}$selector${NC}"
    echo -e "${BOLD}${BLUE}Description:${NC} $description"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    
    # Run the selector and show first few matches
    echo -e "${GREEN}Matches:${NC}"
    cargo run --quiet --bin wasot -- select "$DEMO_FILE" -s "$selector" 2>/dev/null | head -5
    
    # Count total matches
    local count=$(cargo run --quiet --bin wasot -- select "$DEMO_FILE" -s "$selector" 2>/dev/null | grep -c "Matched selector" || true)
    echo -e "${BOLD}Total matches: ${count}${NC}"
}

# Header
echo -e "${BOLD}${GREEN}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${GREEN}║         Wasot Integrated Selector Usage Demo              ║${NC}"
echo -e "${BOLD}${GREEN}╚════════════════════════════════════════════════════════════╝${NC}"
echo -e "\n${YELLOW}Using demo file: ${DEMO_FILE}${NC}"

# Build the project first
echo -e "\n${BLUE}Building project...${NC}"
cargo build --quiet --bin wasot

# Basic Selectors
echo -e "\n${BOLD}${GREEN}═══ BASIC SELECTORS ═══${NC}"

# Universal selector
run_selector "*" "Universal selector - matches all blocks"

# Type selectors
run_selector "paragraph" "Type selector - matches all 'paragraph' blocks"
run_selector "code" "Type selector - matches all 'code' blocks"
run_selector "section:features" "Type selector - matches block with type 'section:features'"
run_selector "data:dataset-1" "Type selector - matches block with type 'data:dataset-1'"
run_selector "list:shopping" "Type selector - matches block with type 'list:shopping'"

# Class selectors (dot-prefixed positional arguments)
echo -e "\n${BOLD}${GREEN}═══ CLASS SELECTORS ═══${NC}"
echo -e "${YELLOW}Note: Classes are positional arguments prefixed with a dot${NC}"

run_selector ".important" "Class selector - matches blocks with class 'important'"
run_selector ".highlight" "Class selector - matches blocks with class 'highlight'"
run_selector ".primary" "Class selector - matches blocks with class 'primary'"
run_selector ".ordered" "Class selector - matches blocks with class 'ordered'"
run_selector ".completed" "Class selector - matches blocks with class 'completed'"

# ID selectors (auto-generated IDs)
echo -e "\n${BOLD}${GREEN}═══ ID SELECTORS ═══${NC}"
echo -e "${YELLOW}Note: IDs are auto-generated in Wasot unless explicitly specified${NC}"

run_selector "#1" "ID selector - matches first block (auto-generated ID)"
run_selector "#g" "ID selector - matches block with auto-generated id 'g'"
run_selector "#h" "ID selector - matches block with auto-generated id 'h'"

# Argument selectors
echo -e "\n${BOLD}${GREEN}═══ ARGUMENT SELECTORS ═══${NC}"

# Has argument (positional or keyword)
run_selector "[important]" "Has argument - matches blocks with 'important' argument"
run_selector "[language]" "Has argument - matches blocks with 'language' keyword"
run_selector "[author]" "Has argument - matches blocks with 'author' keyword"
run_selector "[type]" "Has argument - matches blocks with 'type' keyword"
run_selector "[format]" "Has argument - matches blocks with 'format' keyword"

# Doesn't have argument
run_selector "[!width]" "Not has argument - matches blocks WITHOUT 'width' keyword"
run_selector "[!private]" "Not has argument - matches blocks WITHOUT 'private' argument"
run_selector "[!year]" "Not has argument - matches blocks WITHOUT 'year' keyword"

# Has specific key=value
run_selector '[language="python"]' "Key=value selector - matches blocks with language='python'"
run_selector '[language="rust"]' "Key=value selector - matches blocks with language='rust'"
run_selector '[type="ordered"]' "Key=value selector - matches blocks with type='ordered'"
run_selector '[format="json"]' "Key=value selector - matches blocks with format='json'"
run_selector '[level="advanced"]' "Key=value selector - matches blocks with level='advanced'"
run_selector '[year="2024"]' "Key=value selector - matches blocks with year='2024'"

# Advanced Argument selectors
echo -e "\n${BOLD}${GREEN}═══ ADVANCED ARGUMENT SELECTORS ═══${NC}"

# Has key but not value
run_selector '[level~="beginner"]' "Negated match - matches blocks with 'level' NOT equal to 'beginner'"
run_selector '[type~="ordered"]' "Negated match - matches blocks with 'type' NOT equal to 'ordered'"

# Starts with
run_selector '[src^="photo"]' "Starts with - matches blocks where 'src' starts with 'photo'"
run_selector '[filename^="example"]' "Starts with - matches blocks where 'filename' starts with 'example'"

# Ends with
run_selector '[src$=".jpg"]' "Ends with - matches blocks where 'src' ends with '.jpg'"
run_selector '[filename$=".py"]' "Ends with - matches blocks where 'filename' ends with '.py'"

# Contains substring
run_selector '[owner*="Demo"]' "Contains - matches blocks where 'owner' contains 'Demo'"
run_selector '[topic*="combinator"]' "Contains - matches blocks where 'topic' contains 'combinator'"

# Summary
echo -e "\n${BOLD}${GREEN}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${GREEN}║                     Demo Complete!                        ║${NC}"
echo -e "${BOLD}${GREEN}╚════════════════════════════════════════════════════════════╝${NC}"

echo -e "\n${CYAN}Implemented selector types:${NC}"
echo -e "  ${GREEN}✓${NC} Universal selector (${YELLOW}*${NC})"
echo -e "  ${GREEN}✓${NC} Type selectors (${YELLOW}type-name${NC})"
echo -e "  ${GREEN}✓${NC} Class selectors (${YELLOW}.class${NC}) - dot-prefixed positional args"
echo -e "  ${GREEN}✓${NC} ID selectors (${YELLOW}#id${NC})"
echo -e "  ${GREEN}✓${NC} Has argument selectors (${YELLOW}[argument]${NC})"
echo -e "  ${GREEN}✓${NC} Not has argument selectors (${YELLOW}[!argument]${NC})"
echo -e "  ${GREEN}✓${NC} Key=value selectors (${YELLOW}[key=\"value\"]${NC})"
echo -e "  ${GREEN}✓${NC} Key~=value selectors (${YELLOW}[key~=\"value\"]${NC}) - has key, not value"
echo -e "  ${GREEN}✓${NC} Key^=value selectors (${YELLOW}[key^=\"value\"]${NC}) - starts with"
echo -e "  ${GREEN}✓${NC} Key$=value selectors (${YELLOW}[key\$=\"value\"]${NC}) - ends with"
echo -e "  ${GREEN}✓${NC} Key*=value selectors (${YELLOW}[key*=\"value\"]${NC}) - contains"

echo -e "\n${BLUE}Try your own selectors:${NC}"
echo -e "  ${YELLOW}cargo run --bin wasot -- select $DEMO_FILE -s \"your-selector\"${NC}"
echo -e "\n${BLUE}Multiple selectors:${NC}"
echo -e "  ${YELLOW}cargo run --bin wasot -- select $DEMO_FILE -s \"paragraph\" -s \"code\"${NC}"