#!/bin/bash
# Pi Video - Video Generation Tool
# Usage: ./video.sh <command> [args]

set -e

CMD="${1:-help}"
PROJECT="${2:-}"
PROJECT_DIR="${PROJECT_DIR:-./video-projects}"

# Colors
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m'

log() { echo -e "${GREEN}[video]${NC} $1"; }
info() { echo -e "${CYAN}[video]${NC} $1"; }
warn() { echo -e "${YELLOW}[video]${NC} $1"; }

# Check Node.js
check_deps() {
    if ! command -v node &> /dev/null; then
        warn "Node.js not found. Install from nodejs.org"
        exit 1
    fi
    
    info "Node.js: $(node --version)"
}

# Create slideshow project
create_slideshow() {
    local name="$1"
    local dir="$PROJECT_DIR/$name"
    
    log "Creating slideshow: $name"
    mkdir -p "$dir"
    
    cat > "$dir/package.json" << 'EOF'
{
  "name": "slideshow",
  "version": "1.0.0",
  "scripts": {
    "start": "remotion studio",
    "build": "remotion render out/video.mp4"
  },
  "dependencies": {
    "@remotion/cli": "^4.0",
    "remotion": "^4.0"
  }
}
EOF

    cat > "$dir/src/index.tsx" << 'EOF'
import { AbsoluteFill, Sequence, useCurrentFrame } from "remotion";

const Slide: React.FC<{ text: string; duration: number }> = ({ text, duration }) => {
  return (
    <AbsoluteFill
      style={{
        backgroundColor: "#1a1a2e",
        justifyContent: "center",
        alignItems: "center",
        fontSize: 64,
        color: "white",
        fontFamily: "sans-serif",
      }}
    >
      <Sequence from={0} durationInFrames={duration}>
        <div style={{ opacity: 1 }}>{text}</div>
      </Sequence>
    </AbsoluteFill>
  );
};

export default function Slideshow() {
  return (
    <AbsoluteFill style={{ backgroundColor: "#0f0f23" }}>
      <Slide text="Slide 1" duration={30} />
      <Slide text="Slide 2" duration={30} />
      <Slide text="Slide 3" duration={30} />
    </AbsoluteFill>
  );
}
EOF

    log "Created: $dir"
    log "Run: cd $dir && npm install && npm start"
}

# Create timeline project
create_timeline() {
    local name="$1"
    local dir="$PROJECT_DIR/$name"
    
    log "Creating timeline: $name"
    mkdir -p "$dir"
    
    cat > "$dir/package.json" << 'EOF'
{
  "name": "timeline",
  "version": "1.0.0",
  "scripts": {
    "start": "remotion studio",
    "build": "remotion render out/video.mp4"
  },
  "dependencies": {
    "@remotion/cli": "^4.0",
    "remotion": "^4.0"
  }
}
EOF

    cat > "$dir/src/index.tsx" << 'EOF'
import { AbsoluteFill, useCurrentFrame, interpolate } from "remotion";

export default function Timeline() {
  const frame = useCurrentFrame();
  const progress = interpolate(frame, [0, 150], [0, 100]);
  
  return (
    <AbsoluteFill style={{ backgroundColor: "#1a1a2e", justifyContent: "center", alignItems: "center" }}>
      <div style={{ color: "white", fontSize: 48, fontFamily: "sans-serif" }}>
        Timeline Progress
      </div>
      <div style={{ 
        width: 400, 
        height: 20, 
        backgroundColor: "#333", 
        borderRadius: 10,
        marginTop: 20 
      }}>
        <div style={{ 
          width: `${progress}%`, 
          height: "100%", 
          backgroundColor: "#00d4ff",
          borderRadius: 10 
        }} />
      </div>
      <div style={{ color: "white", marginTop: 10 }}>
        {progress.toFixed(0)}%
      </div>
    </AbsoluteFill>
  );
}
EOF

    log "Created: $dir"
}

# Help
show_help() {
    echo "Pi Video - Generate videos with Pi"
    echo ""
    echo "Commands:"
    echo "  slideshow <name>    Create slideshow project"
    echo "  timeline <name>     Create timeline project"
    echo "  list               List projects"
    echo "  help                Show this help"
    echo ""
    echo "Examples:"
    echo "  ./video.sh slideshow my-presentation"
    echo "  ./video.sh timeline progress-video"
}

# Main
main() {
    check_deps
    
    case "$CMD" in
        slideshow)
            create_slideshow "$PROJECT"
            ;;
        timeline)
            create_timeline "$PROJECT"
            ;;
        list)
            ls "$PROJECT_DIR" 2>/dev/null || echo "No projects"
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            show_help
            ;;
    esac
}

main "$@"