[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [string]$Url,
    [int]$MaxPages = 12,
    [int]$MaxDepth = 2,
    [switch]$PromptCookie,
    [switch]$FetchOnly,
    [string]$CookieStore = "plugins/1c/rag/official-docs/.local/its-cookie.dpapi.txt",
    [string]$RawDir = "plugins/1c/rag/official-docs/raw",
    [string]$NormalizedDir = "plugins/1c/rag/official-docs/normalized",
    [string]$RagSourceDir = "plugins/1c/rag/sources/official/its"
)

$ErrorActionPreference = "Stop"

Add-Type -AssemblyName System.Security

function Unprotect-Text([string]$EncryptedText) {
    $bytes = [Convert]::FromBase64String($EncryptedText.Trim())
    $plain = [System.Security.Cryptography.ProtectedData]::Unprotect(
        $bytes,
        $null,
        [System.Security.Cryptography.DataProtectionScope]::CurrentUser
    )
    return [System.Text.Encoding]::UTF8.GetString($plain)
}

if ($PromptCookie -or -not (Test-Path -LiteralPath $CookieStore)) {
    & (Join-Path $PSScriptRoot "set_1c_its_cookie.ps1") -CookieStore $CookieStore
}

if (-not (Test-Path -LiteralPath $CookieStore)) {
    throw "Cookie store was not created: $CookieStore"
}

$env:ONEC_ITS_COOKIE = Unprotect-Text (Get-Content -LiteralPath $CookieStore -Raw -Encoding UTF8)
if (-not $env:ONEC_ITS_COOKIE) {
    throw "Stored 1C:ITS cookie is empty. Run with -PromptCookie."
}

$rawManifest = Join-Path $RawDir "manifest.json"
$normalizedManifest = Join-Path $NormalizedDir "manifest.json"

python -X utf8 scripts/fetch_1c_its_target_doc.py $Url `
    --output-dir $RawDir `
    --manifest $rawManifest `
    --max-pages $MaxPages `
    --max-depth $MaxDepth `
    --print

if ($FetchOnly) {
    Write-Host "Target fetch completed. Raw manifest: $rawManifest"
    exit 0
}

python -X utf8 scripts/normalize_1c_its_docs.py `
    --manifest $rawManifest `
    --raw-dir $RawDir `
    --output-dir $NormalizedDir `
    --rag-source-dir $RagSourceDir `
    --manifest-output $normalizedManifest

python -X utf8 scripts/prepare_1c_rag_corpus.py
python -X utf8 scripts/build_1c_rag_index.py

Write-Host "1C:ITS target document pipeline completed."
Write-Host "Raw manifest: $rawManifest"
Write-Host "Normalized manifest: $normalizedManifest"
