# PowerShell script to apply database migration via Supabase CLI
# This connects to your remote Supabase project and applies the search function update

$PROJECT_REF = "zwkihedetedlatyvplyz"
$MIGRATION_FILE = "migrations/update_search_function.sql"

Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "Sojorn Database Migration" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "This will update the search_sojorn() function to enable:" -ForegroundColor Yellow
Write-Host "  - Full-text search in post bodies" -ForegroundColor White
Write-Host "  - User search by display name AND handle" -ForegroundColor White
Write-Host "  - Hashtag search with post results" -ForegroundColor White
Write-Host ""

# Check if Supabase CLI is installed
if (-not (Get-Command supabase -ErrorAction SilentlyContinue)) {
    Write-Host "ERROR: Supabase CLI is not installed or not in PATH" -ForegroundColor Red
    Write-Host "Install via: scoop install supabase" -ForegroundColor Yellow
    Write-Host "Or visit: https://supabase.com/docs/guides/cli" -ForegroundColor Yellow
    exit 1
}

Write-Host "Step 1: Linking to Supabase project..." -ForegroundColor Green
Write-Host "Project Reference: $PROJECT_REF" -ForegroundColor White

# Link to project (will prompt for database password if needed)
$linkResult = supabase link --project-ref $PROJECT_REF 2>&1

if ($LASTEXITCODE -ne 0) {
    Write-Host ""
    Write-Host "ERROR: Failed to link to Supabase project" -ForegroundColor Red
    Write-Host "Please ensure you have the correct project reference and database password" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "Alternative: Apply via Supabase Dashboard" -ForegroundColor Cyan
    Write-Host "1. Go to: https://app.supabase.com/project/$PROJECT_REF/sql" -ForegroundColor White
    Write-Host "2. Copy contents of: $MIGRATION_FILE" -ForegroundColor White
    Write-Host "3. Paste and run in SQL Editor" -ForegroundColor White
    exit 1
}

Write-Host ""
Write-Host "Step 2: Applying migration..." -ForegroundColor Green
Write-Host "Reading: $MIGRATION_FILE" -ForegroundColor White

# Read the migration SQL
if (-not (Test-Path $MIGRATION_FILE)) {
    Write-Host "ERROR: Migration file not found: $MIGRATION_FILE" -ForegroundColor Red
    exit 1
}

$sql = Get-Content $MIGRATION_FILE -Raw

# Apply the migration
Write-Host "Executing SQL..." -ForegroundColor White
$result = $sql | supabase db execute 2>&1

if ($LASTEXITCODE -eq 0) {
    Write-Host ""
    Write-Host "SUCCESS! Migration applied successfully" -ForegroundColor Green
    Write-Host ""
    Write-Host "Next steps:" -ForegroundColor Cyan
    Write-Host "1. Test the search functionality in your app" -ForegroundColor White
    Write-Host "2. Search for users, hashtags, and words in posts" -ForegroundColor White
    Write-Host "3. Click hashtags in posts to navigate to search" -ForegroundColor White
} else {
    Write-Host ""
    Write-Host "ERROR: Migration failed" -ForegroundColor Red
    Write-Host "Error output:" -ForegroundColor Yellow
    Write-Host $result
    Write-Host ""
    Write-Host "Try applying manually via Supabase Dashboard:" -ForegroundColor Cyan
    Write-Host "https://app.supabase.com/project/$PROJECT_REF/sql" -ForegroundColor White
    exit 1
}
