# Production deployment script for rollover enhancements
# Handles migration issues and deploys the new rollover functionality

Write-Host "=== Production Deployment: Rollover Enhancements ===" -ForegroundColor Green
Write-Host "This script will deploy the enhanced rollover functionality with date field" -ForegroundColor Yellow
Write-Host "and add Complete History buttons to client popup and loan detail pages." -ForegroundColor Yellow
Write-Host ""

# Set error handling
$ErrorActionPreference = "Stop"

try {
    # Navigate to project directory (adjust path as needed)
    $projectPath = "/home/acbptxvs/public_html/branchbusinessadvance.co.ke"
    Write-Host "=== Step 1: Setting up environment ===" -ForegroundColor Cyan
    
    # Activate virtual environment
    $venvPath = "/home/acbptxvs/virtualenv/public_html/branchbusinessadvance.co.ke/3.13/bin/activate"
    Write-Host "Activating virtual environment..." -ForegroundColor Yellow
    
    Write-Host "=== Step 2: Running Python deployment script ===" -ForegroundColor Cyan
    python deploy_rollover_enhancements.py
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host ""
        Write-Host "=== Deployment Complete ===" -ForegroundColor Green
        Write-Host "SUCCESS: All rollover enhancements have been deployed!" -ForegroundColor Green
        Write-Host "Features deployed:" -ForegroundColor Yellow
        Write-Host "  - Enhanced rollover form with date field" -ForegroundColor White
        Write-Host "  - Complete History button in client popup" -ForegroundColor White
        Write-Host "  - Complete History button in loan detail pages" -ForegroundColor White
        Write-Host "  - Database migration for rollover_date field" -ForegroundColor White
        Write-Host ""
        Write-Host "The system is now ready with the enhanced rollover functionality!" -ForegroundColor Green
    } else {
        Write-Host "ERROR: Deployment failed with exit code $LASTEXITCODE" -ForegroundColor Red
        exit 1
    }
    
} catch {
    Write-Host "ERROR: Deployment failed with exception: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "Please check the error above and try again." -ForegroundColor Red
    exit 1
}
