$root = 'd:\laragon\www\lostfound_relink'
$files = Get-ChildItem -Path $root -Recurse -Include '*.php' | Where-Object {
    $_.FullName -notmatch '\\config\\db\.php' -and
    $_.FullName -notmatch '\\config\\auth\.php' -and
    $_.FullName -notmatch '\\backups\\'
}

foreach ($file in $files) {
    $content = [System.IO.File]::ReadAllText($file.FullName)
    if ($content -notmatch '/lostfound_relink/') { continue }

    # CSS and JS asset links (inside href/src attributes)
    $content = $content -replace '/lostfound_relink/assets/css/style\.css', '<?= BASE_URL ?>/assets/css/style.css'
    $content = $content -replace '/lostfound_relink/assets/js/main\.js', '<?= BASE_URL ?>/assets/js/main.js'

    # header() redirects
    $content = $content -replace '(header\("Location: )/lostfound_relink/', '$1" . BASE_URL . "/'

    # href/src inside HTML strings like href="/lostfound_relink/..."
    $content = $content -replace '(href|src|action)="\/lostfound_relink\/', '$1="<?= BASE_URL ?>/'

    # PHP image paths inside echo/print like: src="/lostfound_relink/<?=
    $content = $content -replace '"\/lostfound_relink\/<\?=', '"<?= BASE_URL ?>/<?='

    # JS string paths like '/lostfound_relink/...' or "/lostfound_relink/..."
    $content = $content -replace "'\/lostfound_relink\/", "'" + '<?= BASE_URL ?>/'
    $content = $content -replace 'const AJAX = "\/lostfound_relink\/', 'const AJAX = "<?= BASE_URL ?>/'

    # JS template literals: `/lostfound_relink/${
    $content = $content -replace '`\/lostfound_relink\/\$\{', '`${BASE_URL}/${'

    # JS template literals: src="/lostfound_relink/${
    $content = $content -replace '"\/lostfound_relink\/\$\{', '"${BASE_URL}/${'

    # file_exists / unlink paths using DOCUMENT_ROOT
    $content = $content -replace "\\\$_SERVER\['DOCUMENT_ROOT'\] \. '\/lostfound_relink\/'", "`$_SERVER['DOCUMENT_ROOT'] . BASE_URL . '/'"

    # backup_dir using DOCUMENT_ROOT
    $content = $content -replace "\\\$backup_dir = \\\$_SERVER\['DOCUMENT_ROOT'\] \. '\/lostfound_relink\/backups\/'", '$backup_dir = $_SERVER[''DOCUMENT_ROOT''] . BASE_URL . ''/backups/'''

    [System.IO.File]::WriteAllText($file.FullName, $content)
    Write-Host "Updated: $($file.Name)"
}
Write-Host "Done."
