#!/bin/bash
# Get Figma file info
# Usage: ./figma-get-file.sh <file-key-or-url>

set -e

INPUT="${1:?File key or URL required}"
WORKSPACE="/home/aria/.openclaw/workspace"

# Load environment
eval $($WORKSPACE/scripts/load-env.sh --export)

# Extract file key if URL provided
if [[ "$INPUT" =~ ^https:// ]]; then
    FILE_KEY=$(echo "$INPUT" | sed -E 's|.*/design/([^/]+)/.*|\1|' | sed -E 's|.*/file/([^/]+)/.*|\1|')
else
    FILE_KEY="$INPUT"
fi

echo "Fetching Figma file: $FILE_KEY"
echo ""

# Get file info
curl -s -H "X-Figma-Token: $FIGMA_ACCESS_TOKEN" \
    "https://api.figma.com/v1/files/$FILE_KEY?depth=1" | jq '{
        name: .name,
        lastModified: .lastModified,
        version: .version,
        pages: [.document.children[] | {name: .name, id: .id}]
    }'
