# Start: Local Environment Configuration

> Reference for step-start.md — auto-generated local config for each worktree.

## Port Generation

```bash
# Generate unique ports based on branch name hash (to avoid conflicts between worktrees)
HASH=$(echo "$BRANCH_NAME" | shasum 2>/dev/null || echo "$BRANCH_NAME" | md5sum 2>/dev/null || echo "$BRANCH_NAME" | cksum)
HASH=$(echo "$HASH" | cut -c1-4)
HASH_NUM=$((16#$HASH % 100))
API_PORT=$((5200 + HASH_NUM))
WEB_PORT=$((5300 + HASH_NUM))

PROJECT_NAME=$(echo "$GF_PROJECT_NAME" | tr '[:upper:]' '[:lower:]')
DB_NAME=$(echo "${PROJECT_NAME}_${branch_type}_${BRANCH_NAME}" | cut -c1-63)
```

## 7a. Backend: appsettings.Local.json

```bash
[ -n "$API_DIR" ] && [ -f "$API_DIR/appsettings.json" ] && {
  cat > "$API_DIR/appsettings.Local.json" << EOF
{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=${DB_NAME};Trusted_Connection=true;TrustServerCertificate=true"
  },
  "SmartStack": {
    "AutoMigrate": true,
    "FailOnMigrationError": true,
    "EnableDevSeeding": true
  },
  "Authentication": {
    "FrontendUrl": "http://localhost:${WEB_PORT}"
  },
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:${API_PORT}"
      }
    }
  },
  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug"
    }
  }
}
EOF
}
```

## 7b. Frontend: .env.standalone + npm script

> **IMPORTANT**: Vite 7+ forbids `"local"` as a mode name (conflicts with `.env.local` suffix).
> The Vite mode is `standalone`, but the npm script stays `local` for user convenience.

```bash
[ -n "$WEB_DIR" ] && [ -f "$WEB_DIR/package.json" ] && {
  cat > "$WEB_DIR/.env.standalone" << EOF
VITE_API_URL=http://localhost:${API_PORT}
VITE_APP_ENV=local
VITE_DEBUG=true
EOF

  # Add "local" script to package.json if not exists
  if ! grep -q '"local"' "$WEB_DIR/package.json"; then
    node -e "
      const fs = require('fs');
      const pkg = JSON.parse(fs.readFileSync('$WEB_DIR/package.json', 'utf8'));
      if (!pkg.scripts.local) {
        pkg.scripts.local = 'vite --mode standalone --port ${WEB_PORT}';
        fs.writeFileSync('$WEB_DIR/package.json', JSON.stringify(pkg, null, 2) + '\n');
      }
    "
  fi
}
```

## 7c. Backend: launchSettings.json Local Profile

```bash
LAUNCH_SETTINGS="$API_DIR/Properties/launchSettings.json"
[ -n "$API_DIR" ] && [ -f "$LAUNCH_SETTINGS" ] && {
  node -e "
    const fs = require('fs');
    const ls = JSON.parse(fs.readFileSync('$LAUNCH_SETTINGS', 'utf8'));
    ls.profiles = ls.profiles || {};
    ls.profiles.Local = {
      commandName: 'Project',
      dotnetRunMessages: true,
      launchBrowser: false,
      applicationUrl: 'http://localhost:${API_PORT}',
      environmentVariables: {
        ASPNETCORE_ENVIRONMENT: 'Local'
      }
    };
    fs.writeFileSync('$LAUNCH_SETTINGS', JSON.stringify(ls, null, 2) + '\n');
  "
}
```

## 7d. Summary Display

```
┌─────────────────────────────────────────────────────────────┐
│  LOCAL ENVIRONMENT CONFIGURED                               │
├─────────────────────────────────────────────────────────────┤
│  Database:  ${DB_NAME}                                      │
│  API Port:  ${API_PORT}  →  dotnet run --launch-profile Local│
│  Web Port:  ${WEB_PORT}  →  npm run local                   │
└─────────────────────────────────────────────────────────────┘
```
