Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# Load shared utilities
$utilsPath = Join-Path $PSScriptRoot '..\lib\utils.ps1'
. $utilsPath

.\scripts\Windows\starter.ps1
if ($LASTEXITCODE -ne 0) {
    exit $LASTEXITCODE
}

if (-not (Test-Path '.env'))
{
    Write-Warning-Custom ".env file not found. Exiting without running Docker steps."
    exit 0
}

# Remove old fuzzy entries
Write-Step "Remove old fuzzy entries..."
docker compose exec app uv run manage.py fix_fuzzy
if ($LASTEXITCODE -ne 0) {
    Write-Error-Custom "Failed to remove old fuzzy entries"
    exit $LASTEXITCODE
}
Write-Success "Old fuzzy entries removed successfully!"

# Update PO files
Write-Step "Updating PO files..."
docker compose exec app uv run manage.py makemessages -l ar_AR -l cs_CZ -l da_DK -l de_DE -l en_GB -l en_US -l es_ES -l fa_IR -l fr_FR -l he_IL -l hi_IN -l hr_HR -l id_ID -l it_IT -l ja_JP -l kk_KZ -l ko_KR -l nl_NL -l no_NO -l pl_PL -l pt_BR -l ro_RO -l ru_RU -l sv_SE -l th_TH -l tr_TR -l vi_VN -l zh_Hans
if ($LASTEXITCODE -ne 0) {
    Write-Error-Custom "Failed to update PO files"
    exit $LASTEXITCODE
}
Write-Success "PO files updated successfully!"

# Fix new fuzzy entries
Write-Step "Fixing new fuzzy entries..."
docker compose exec app uv run manage.py fix_fuzzy
if ($LASTEXITCODE -ne 0) {
    Write-Error-Custom "Failed to fix new fuzzy entries"
    exit $LASTEXITCODE
}
Write-Success "New fuzzy entries fixed successfully!"

# Translate with DeepL
Write-Step "Translating with DeepL..."
docker compose exec app uv run manage.py deepl_translate -l ALL -a ALL
if ($LASTEXITCODE -ne 0) {
    Write-Error-Custom "Translation failed"
    exit $LASTEXITCODE
}
Write-Success "Translated successfully!"

Write-Result ""
Write-Result "You can now use compile-messages.ps1 script or run: lessy.py compile-messages"
