# JSON Linting, Parsing & Validation Tasks
# Provides JSON linting, parsing, schema validation and common operations

version: '3'

vars:
  DEFAULT_PATTERNS: '*.json,**/*.json,!node_modules/**,!dist/**,!.taskmaster/**'

tasks:
  default:
    desc: 'Show available JSON tasks'
    aliases: [help, h]
    silent: true
    cmds:
      - |
        # Color codes
        GREEN='\033[0;32m'
        YELLOW='\033[0;33m'
        BOLD='\033[1m'
        NC='\033[0m'

        echo -e "${BOLD}JSON Linting, Parsing & Validation${NC}"
        echo ""
        echo "Command                            Alias     Description                              Examples"
        echo "───────────────────────────────────────────────────────────────────────────────────────────────────"
        echo -e "  ${GREEN}task json:lint${NC}                   ${YELLOW}l${NC}         Lint JSON files                      FILES=\"*.json\""
        echo -e "  ${GREEN}task json:lint:fix${NC}               ${YELLOW}lf${NC}        Auto-fix JSON formatting             FILES=\"*.json\""
        echo ""
        echo -e "${BOLD}Usage Examples:${NC}"
        echo -e "  task json:lint                             Lint all JSON files (quiet on success)"
        echo -e "  task json:lint FILES=\"*.json\"              Lint specific pattern"
        echo -e "  VERBOSE=1 task json:lint                   Show output even on success"
        echo -e "  task json:lint:fix                         Auto-fix all issues"

  lint:
    desc: 'Lint JSON files (quiet on success, use VERBOSE=1 for details)'
    aliases: [l]
    silent: true
    vars:
      FILE_PATTERNS: '{{.FILES | default .DEFAULT_PATTERNS}}'
      VERBOSE_FLAG: '{{.VERBOSE | default ""}}'
    cmds:
      - |
        REPO_URL=$(task git:repo:url 2>/dev/null || echo "")
        SCRIPT="{{.TASKFILE_DIR}}/../src/tasks/json/lint.ts"

        if [ "$REPO_URL" = "https://github.com/northbridge-security/ai-toolkit" ] && [ -f "$SCRIPT" ]; then
          bun run "$SCRIPT"
        else
          # Fallback to ai-toolkit CLI
          npx @northbridge-security/ai-toolkit json lint -f "$FILES"
        fi
    env:
      FILES: '{{.FILE_PATTERNS}}'
      VERBOSE: '{{.VERBOSE_FLAG}}'

  lint:fix:
    desc: 'Auto-fix JSON formatting (pretty-print with 2-space indent)'
    aliases: [lf, fix]
    silent: true
    vars:
      FILE_PATTERNS: '{{.FILES | default .DEFAULT_PATTERNS}}'
      VERBOSE_FLAG: '{{.VERBOSE | default ""}}'
    cmds:
      - |
        REPO_URL=$(task git:repo:url 2>/dev/null || echo "")
        SCRIPT="{{.TASKFILE_DIR}}/../src/tasks/json/lint-fix.ts"

        if [ "$REPO_URL" = "https://github.com/northbridge-security/ai-toolkit" ] && [ -f "$SCRIPT" ]; then
          bun run "$SCRIPT"
        else
          # Fallback to ai-toolkit CLI
          npx @northbridge-security/ai-toolkit json lint --fix -f "$FILES"
        fi
    env:
      FILES: '{{.FILE_PATTERNS}}'
      VERBOSE: '{{.VERBOSE_FLAG}}'
