# YAML Linting & Validation Tasks
# Provides YAML validation, linting, and auto-fixing for all YAML files in the project.

version: '3'

vars:
  DEFAULT_PATTERNS: '*.yml,*.yaml,.*.yml,.*.yaml,.github/**/*.yml,.github/**/*.yaml,tasks/**/*.yml'
  YAMLLINT_CONFIG: '.yamllint.yml'

tasks:
  default:
    desc: 'Show available YAML 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}YAML Linting & Validation${NC}"
        echo ""
        echo "Command                            Alias     Description                              Examples"
        echo "───────────────────────────────────────────────────────────────────────────────────────────────────"
        echo -e "  ${GREEN}task yaml:lint${NC}                   ${YELLOW}l${NC}         Lint YAML files                          FILES=\"*.yml\""
        echo -e "  ${GREEN}task yaml:lint:fix${NC}               ${YELLOW}lf${NC}        Auto-fix YAML formatting                 FILES=\"*.yml\""
        echo -e "  ${GREEN}task yaml:validate${NC}               ${YELLOW}v${NC}         Validate YAML syntax only                FILES=\"*.yml\""
        echo -e "  ${GREEN}task yaml:setup${NC}                  ${YELLOW}s${NC}         Install YAML tools globally"
        echo ""
        echo -e "${BOLD}Usage Examples:${NC}"
        echo -e "  task yaml:lint                            # Lint all YAML files (quiet on success)"
        echo -e "  task yaml:lint FILES=\"*.yml\"              # Lint specific pattern"
        echo -e "  task yaml:lint LINTER=prettier            # Force prettier"
        echo -e "  VERBOSE=1 task yaml:lint                  # Show output even on success"
        echo -e "  task yaml:lint:fix                        # Auto-fix all issues"

  lint:
    desc: 'Lint YAML files (quiet on success, use VERBOSE=1 for details)'
    aliases: [l]
    silent: true
    vars:
      FILE_PATTERNS: '{{.FILES | default .DEFAULT_PATTERNS}}'
      LINTER: '{{.LINTER | default ""}}'
      VERBOSE_FLAG: '{{.VERBOSE | default ""}}'
    cmds:
      - |
        REPO_URL=$(task git:repo:url 2>/dev/null || echo "")
        SCRIPT="{{.TASKFILE_DIR}}/../src/tasks/yaml/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 yaml lint -f "$FILES"
        fi
    env:
      FILES: '{{.FILE_PATTERNS}}'
      LINTER: '{{.LINTER}}'
      VERBOSE: '{{.VERBOSE_FLAG}}'

  lint:fix:
    desc: 'Auto-fix YAML files where possible (quiet when no changes)'
    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/yaml/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 yaml lint --fix -f "$FILES"
        fi
    env:
      FILES: '{{.FILE_PATTERNS}}'
      VERBOSE: '{{.VERBOSE_FLAG}}'

  validate:
    desc: 'Validate YAML syntax (quiet on success, use VERBOSE=1 for details)'
    aliases: [v]
    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/yaml/validate.ts"

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

  setup:
    desc: 'Install YAML tools globally (js-yaml, prettier, yamllint)'
    aliases: [s]
    silent: true
    cmds:
      - |
        REPO_URL=$(task git:repo:url 2>/dev/null || echo "")
        SCRIPT="{{.TASKFILE_DIR}}/../src/tasks/yaml/setup.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 yaml setup
        fi
