# Malware Website Crawler - AWS Teardown Script (PowerShell)
# This script will destroy all AWS resources created by Terraform

param(
    [switch]$Force,
    [string]$Environment = "prod"
)

Write-Host "🚨 WARNING: This script will destroy ALL AWS resources for the Malware Website Crawler!" -ForegroundColor Red
Write-Host "This action cannot be undone and will result in permanent data loss." -ForegroundColor Red
Write-Host ""

if (-not $Force) {
    $confirm = Read-Host "Are you sure you want to continue? (yes/no)"
    if ($confirm -ne "yes") {
        Write-Host "❌ Teardown cancelled." -ForegroundColor Yellow
        exit 1
    }
}

Write-Host ""
Write-Host "🔍 Starting AWS resource teardown..." -ForegroundColor Green

# Change to terraform directory
$terraformDir = Join-Path $PSScriptRoot "..\terraform"
Set-Location $terraformDir

# Check if terraform is initialized
if (-not (Test-Path ".terraform")) {
    Write-Host "📦 Initializing Terraform..." -ForegroundColor Blue
    terraform init
}

# Get current state
Write-Host "📊 Current Terraform state:" -ForegroundColor Blue
try {
    $state = terraform show -json | ConvertFrom-Json
    if ($state.values.root_module.resources) {
        $state.values.root_module.resources | Where-Object { $_.type -ne "data" } | ForEach-Object {
            Write-Host "   $($_.type).$($_.name)" -ForegroundColor Gray
        }
    } else {
        Write-Host "   No resources found" -ForegroundColor Gray
    }
} catch {
    Write-Host "   Could not retrieve state information" -ForegroundColor Gray
}

Write-Host ""
Write-Host "🗑️  Destroying AWS resources..." -ForegroundColor Red

# Run terraform destroy with auto-approve
terraform destroy -auto-approve -var="environment=$Environment"

Write-Host ""
Write-Host "🧹 Cleaning up local files..." -ForegroundColor Blue

# Remove terraform state files
Remove-Item -Path "terraform.tfstate*" -Force -ErrorAction SilentlyContinue
Remove-Item -Path ".terraform.lock.hcl" -Force -ErrorAction SilentlyContinue
Remove-Item -Path ".terraform" -Recurse -Force -ErrorAction SilentlyContinue

# Remove any local environment files
Remove-Item -Path "terraform.tfvars" -Force -ErrorAction SilentlyContinue
Remove-Item -Path ".env" -Force -ErrorAction SilentlyContinue

Write-Host ""
Write-Host "✅ Teardown completed successfully!" -ForegroundColor Green
Write-Host ""
Write-Host "📋 Summary of destroyed resources:" -ForegroundColor Cyan
Write-Host "   - VPC and networking components" -ForegroundColor White
Write-Host "   - ECS cluster and services" -ForegroundColor White
Write-Host "   - RDS PostgreSQL database" -ForegroundColor White
Write-Host "   - ElastiCache Redis cluster" -ForegroundColor White
Write-Host "   - S3 buckets (files and logs)" -ForegroundColor White
Write-Host "   - Application Load Balancer" -ForegroundColor White
Write-Host "   - Security groups and IAM roles" -ForegroundColor White
Write-Host "   - CloudWatch log groups" -ForegroundColor White
Write-Host "   - ECR repository" -ForegroundColor White
Write-Host ""
Write-Host "💰 You should see reduced AWS costs in your next billing cycle." -ForegroundColor Green
Write-Host "💡 Remember to check your AWS console to ensure all resources are deleted." -ForegroundColor Yellow
