#!/bin/bash
# Local deployment script for K-ArtSell Aegis
# Usage: ./scripts/deploy-local.sh

set -e

echo "═══════════════════════════════════════════════════════"
echo "K-ArtSell Aegis Local Deployment"
echo "═══════════════════════════════════════════════════════"

# Check prerequisites
if ! command -v dotnet &> /dev/null; then
    echo "❌ .NET SDK not found"
    exit 1
fi

# Build
echo "📦 Building project..."
dotnet build KArtSell.sln -c Release --no-restore

# Test
echo "✅ Running tests..."
dotnet test KArtSell.sln --no-build -c Release --logger trx

# Publish
echo "📤 Publishing application..."
PUBLISH_DIR="/tmp/kartsell-publish"
rm -rf "$PUBLISH_DIR"
dotnet publish -c Release -o "$PUBLISH_DIR" src/KArtSell.Host

echo ""
echo "═══════════════════════════════════════════════════════"
echo "✅ Deployment Package Ready"
echo "═══════════════════════════════════════════════════════"
echo ""
echo "Published to: $PUBLISH_DIR"
echo ""
echo "To run locally:"
echo ""
echo "  # Terminal 1: SSH Tunnel"
echo "  ssh -L 5432:127.0.0.1:5432 kjh2064@178.104.200.7"
echo ""
echo "  # Terminal 2: Start Application"
echo "  cd $PUBLISH_DIR"
echo "  export ASPNETCORE_ENVIRONMENT=Production"
echo "  export KARTSELL_POSTGRES=\"Host=localhost;Port=5432;Database=kartsell;Username=kartsell;Password=kartsell\""
echo "  export KRX_OPENAPI=\"<API-KEY>\""
echo "  export OPENDART_API=\"<API-KEY>\""
echo "  export KIS_APP_KEY=\"<KEY>\""
echo "  export KIS_APP_SECRET=\"<SECRET>\""
echo "  dotnet KArtSell.Host.dll"
echo ""
echo "  # Terminal 3: Test API"
echo "  curl -H 'X-KArtSell-User: admin' \\"
echo "       -H 'X-KArtSell-Role: Admin' \\"
echo "       http://127.0.0.1:5002/health"
echo ""
