# dev clean - stop processes, clear caches, optional redis flush / docker deep clean
# Usage:
#   npm run dev:clean
#   npm run dev:clean -- -Deep
#   npm run dev:clean -- -SkipRedis

param(
    [switch]$Deep,
    [switch]$SkipRedis
)

$ErrorActionPreference = "Stop"
. (Join-Path $PSScriptRoot "lib.ps1")

$root = $script:DevConfig.ProjectRoot
Write-DevLog "=== chajia dev clean ===" "Info"
Write-DevLog "root: $root"

Stop-AllDevProcesses
Clear-DevCaches -Root $root

if (-not $SkipRedis) {
    Write-DevLog "flush redis..."
    Invoke-FlushRedis -Root $root
}

Push-Location $root
try {
    if ($Deep) {
        Write-DevLog "deep clean: docker compose down -v" "Warn"
        $code = Invoke-DockerComposeProject down -v
        if ($code -ne 0) {
            Write-DevLog "docker compose down skipped (docker not up?)" "Warn"
        }
        else {
            Write-DevLog "docker volumes removed" "Ok"
        }
    }
}
finally {
    Pop-Location
}

Write-DevLog "clean done. run: npm run dev:start" "Ok"
