version: "3"

vars:
  APP_NAME: maplefile
  WAILS_VERSION: v2.11.0

tasks:
  # Development workflow
  dev:
    desc: Start app in development mode with hot reload (uses local backend)
    env:
      MAPLEFILE_MODE: dev
    cmds:
      - wails dev -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=dev -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=dev"

  dev:production:
    desc: Start app in development mode with production backend
    env:
      MAPLEFILE_MODE: production
    cmds:
      - wails dev -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"

  dev:frontend:
    desc: Start frontend development server only
    dir: frontend
    cmds:
      - npm run dev

  dev:build:
    desc: Build development version (fast, no optimization)
    cmds:
      - wails build -dev

  # Building
  build:
    desc: Build production binary for current platform (production backend)
    cmds:
      - wails build -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"

  build:dev:
    desc: Build binary for development (local backend)
    cmds:
      - wails build -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=dev -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=dev"

  build:all:
    desc: Build for all platforms
    cmds:
      - echo "Building for macOS..."
      - wails build -platform darwin/universal -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"
      - echo "Building for Linux..."
      - wails build -platform linux/amd64 -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"
      - echo "Building for Windows..."
      - wails build -platform windows/amd64 -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"
      - echo "✅ All builds complete"

  build:mac:
    desc: Build for macOS (Universal binary)
    cmds:
      - wails build -platform darwin/universal -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"

  build:linux:
    desc: Build for Linux (amd64)
    cmds:
      - wails build -platform linux/amd64 -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"

  build:windows:
    desc: Build for Windows (amd64)
    cmds:
      - wails build -platform windows/amd64 -ldflags "-X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/app.BuildMode=production -X codeberg.org/mapleopentech/monorepo/native/desktop/maplefile/internal/config.BuildMode=production"

  # Frontend tasks
  frontend:install:
    desc: Install frontend dependencies
    dir: frontend
    cmds:
      - npm install

  frontend:build:
    desc: Build frontend for production
    dir: frontend
    cmds:
      - npm run build

  frontend:lint:
    desc: Lint frontend code
    dir: frontend
    cmds:
      - npm run lint

  frontend:clean:
    desc: Clean frontend build artifacts and dependencies
    dir: frontend
    cmds:
      - rm -rf node_modules dist

  # Go tasks
  go:tidy:
    desc: Tidy Go modules
    cmds:
      - go mod tidy

  go:vendor:
    desc: Vendor Go dependencies
    cmds:
      - go mod vendor

  go:test:
    desc: Run Go tests
    cmds:
      - go test ./... -v

  go:lint:
    desc: Run Go linters
    cmds:
      - go vet ./...

  go:nilaway:
    desc: Run nilaway static analysis for nil pointer dereferences
    cmds:
      - go run go.uber.org/nilaway/cmd/nilaway ./...

  go:format:
    desc: Format Go code
    cmds:
      - go fmt ./...

  # Wails tasks
  wails:doctor:
    desc: Check Wails installation and dependencies
    cmds:
      - wails doctor

  wails:version:
    desc: Show Wails version
    cmds:
      - wails version

  # Utility tasks
  clean:
    desc: Clean build artifacts
    cmds:
      - rm -rf build/bin
      - rm -rf frontend/dist
      - echo "✅ Build artifacts cleaned"

  clean:all:
    desc: Clean all build artifacts and dependencies
    deps: [clean]
    cmds:
      - rm -rf frontend/node_modules
      - rm -rf vendor
      - echo "✅ All artifacts and dependencies cleaned"

  setup:
    desc: Initial project setup (install dependencies)
    cmds:
      - echo "📦 Installing frontend dependencies..."
      - task: frontend:install
      - echo "📦 Tidying Go modules..."
      - task: go:tidy
      - echo "✅ Setup complete! Run 'task dev' to start development"

  check:
    desc: Run all checks (format, lint, nilaway, test)
    cmds:
      - echo "🔍 Formatting Go code..."
      - task: go:format
      - echo "🔍 Linting Go code..."
      - task: go:lint
      - echo "🔍 Running nilaway analysis..."
      - task: go:nilaway
      - echo "🔍 Running tests..."
      - task: go:test
      - echo "🔍 Linting frontend..."
      - task: frontend:lint
      - echo "✅ All checks passed"

  # Package tasks
  package:
    desc: Package the application for distribution
    deps: [build]
    cmds:
      - echo "📦 Packaging application..."
      - echo "Binary located in build/bin/"
      - echo "✅ Package complete"

  # Development helpers
  info:
    desc: Show project information
    cmds:
      - echo "Application {{.APP_NAME}}"
      - echo "Wails Version {{.WAILS_VERSION}}"
      - wails version
      - echo ""
      - echo "Build directory build/bin"

  # Default task
  default:
    desc: Show available tasks
    cmds:
      - task --list
