param(
    [Parameter(Mandatory=$true)]
    [string]$video_url
)

# Function to sanitize strings for folder names
function Get-SafeFolderName {
    param([string]$name)
    $invalid = [System.IO.Path]::GetInvalidFileNameChars()
    $regex = "[{0}]" -f [regex]::Escape(-join $invalid)
    return ($name -replace $regex, "_").Trim()
}

# Function to check if URL is valid YouTube URL
function Test-YouTubeUrl {
    param([string]$url)
    $pattern = '^(https?:\/\/)?(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/)[a-zA-Z0-9_-]{11}.*$'
    return $url -match $pattern
}

# Main script
if (-not (Test-YouTubeUrl $video_url)) {
    Write-Error "Invalid YouTube URL"
    exit 1
}

try {
    # Get video title for folder name
    Write-Host "Getting video information..." -ForegroundColor Green
    $video_info = yt-dlp --get-title --get-id $video_url
    $video_title = $video_info[0]
    $video_id = $video_info[1]
    
    # Create safe folder name
    $folder_name = Get-SafeFolderName $video_title
    $folder_path = Join-Path $PSScriptRoot $folder_name
    
    # Create folder
    Write-Host "Creating folder: $folder_name" -ForegroundColor Yellow
    New-Item -ItemType Directory -Path $folder_path -Force | Out-Null
    
    # Change to video folder
    Push-Location $folder_path
    
    # Download description
    Write-Host "Downloading video description..." -ForegroundColor Yellow
    yt-dlp --skip-download --write-description $video_url
    
    # Download comments
    Write-Host "Downloading comments..." -ForegroundColor Yellow
    yt-dlp --skip-download --write-comments --extractor-args "youtube:max_comments=100;comment_sort=top" $video_url
    
    # Download auto-generated subtitles
    Write-Host "Downloading transcript..." -ForegroundColor Yellow
    yt-dlp --skip-download --write-auto-sub --sub-format vtt $video_url
    
    # Create info.json with metadata
    Write-Host "Saving video metadata..." -ForegroundColor Yellow
    $info = @{
        title = $video_title
        video_id = $video_id
        url = $video_url
        download_date = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
    }
    $info | ConvertTo-Json | Out-File -FilePath "info.json"
    
    # Return to original directory
    Pop-Location
    
    Write-Host "Done! Data saved in: $folder_path" -ForegroundColor Green
    
} catch {
    Write-Error "An error occurred: $_"
    exit 1
}
