from flask import Flask, jsonify, render_template_string
import subprocess
import json

app = Flask(__name__)

@app.route('/')
def index():
    return render_template_string('''
    <!DOCTYPE html>
    <!DOCTYPE html>
    <html>
    <head>
        <title>SMS Campaign Manager - Android Monitor</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="https://cdn.tailwindcss.com"></script>
        <style>
            body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
            .gradient-bg { background: linear-gradient(135deg, #10b981 0%, #059669 100%); }
            .card { background: white; border-radius: 12px; box-shadow: 0 4px 16px rgba(0,0,0,0.1); }
            .nav-link:hover { transform: translateY(-1px); }
        </style>
    </head>
    <body class="bg-gray-50">
        <!-- Header -->
        <div class="gradient-bg text-white p-6 mb-6">
            <div class="max-w-4xl mx-auto">
                <div class="flex justify-between items-center">
                    <div>
                        <h1 class="text-3xl font-bold">📊 Android Monitor</h1>
                        <p class="text-green-100 text-lg">SMS Campaign Manager • Android Interface</p>
                    </div>
                    <div class="flex gap-3">
                        <a href="http://localhost:5000/" 
                           class="nav-link bg-white/20 px-4 py-2 rounded-full text-white no-underline hover:bg-white/30 transition-all">
                            🏠 Homelab
                        </a>
                        <a href="http://10.0.0.193:5001" 
                           class="nav-link bg-white/20 px-4 py-2 rounded-full text-white no-underline hover:bg-white/30 transition-all">
                            📡 SMS API
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="max-w-4xl mx-auto px-6">
            <!-- Status Card -->
            <div class="card p-6 mb-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-4">✅ Server Status</h2>
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div class="bg-green-50 p-4 rounded-lg border border-green-200">
                        <div class="text-sm text-green-600 font-medium">Flask Server</div>
                        <div class="text-lg font-bold text-green-700">Active</div>
                        <div class="text-xs text-green-600">10.0.0.193:5000</div>
                    </div>
                    <div class="bg-blue-50 p-4 rounded-lg border border-blue-200">
                        <div class="text-sm text-blue-600 font-medium">Environment</div>
                        <div class="text-lg font-bold text-blue-700">Termux</div>
                        <div class="text-xs text-blue-600">Android Runtime</div>
                    </div>
                    <div class="bg-purple-50 p-4 rounded-lg border border-purple-200">
                        <div class="text-sm text-purple-600 font-medium">SMS API</div>
                        <div class="text-lg font-bold text-purple-700">Ready</div>
                        <div class="text-xs text-purple-600">Port 5001</div>
                    </div>
                </div>
            </div>
            
            <!-- API Tests Card -->
            <div class="card p-6 mb-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-4">🧪 Termux API Tests</h2>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <a href="/battery" class="block p-4 bg-yellow-50 rounded-lg border border-yellow-200 hover:bg-yellow-100 transition-colors no-underline">
                        <div class="text-lg">🔋 Battery Status</div>
                        <div class="text-sm text-gray-600 mt-1">Check device battery level and health</div>
                    </a>
                    <a href="/notification" class="block p-4 bg-blue-50 rounded-lg border border-blue-200 hover:bg-blue-100 transition-colors no-underline">
                        <div class="text-lg">🔔 Test Notification</div>
                        <div class="text-sm text-gray-600 mt-1">Send a test system notification</div>
                    </a>
            </div>
        </div>
    </body>
    </html>
    ''')

@app.route('/battery')
def battery():
    try:
        result = subprocess.run(['termux-battery-status'], capture_output=True, text=True)
        battery_data = json.loads(result.stdout)
        return f"""
        <h2>🔋 Battery Status</h2>
        <pre>{json.dumps(battery_data, indent=2)}</pre>
        <p><a href='/'>← Back</a></p>
        """
    except Exception as e:
        return f"<h2>Error</h2><pre>{str(e)}</pre><p><a href='/'>← Back</a></p>"

@app.route('/notification')
def notification():
    try:
        subprocess.run(['termux-notification', '--title', 'Flask Test', '--content', 'Hello from SMS Campaign Manager!'], capture_output=True, text=True)
        return f"""
        <h2>🔔 Notification Sent!</h2>
        <p>Check your Android notifications.</p>
        <p><a href='/'>← Back</a></p>
        """
    except Exception as e:
        return f"<h2>Error</h2><pre>{str(e)}</pre><p><a href='/'>← Back</a></p>"

if __name__ == '__main__':
    print("🚀 Starting SMS Campaign Manager on Termux...")
    print("📱 Device IP: 10.0.0.193")
    print("🌐 Access from Ubuntu: http://10.0.0.193:5000")
    app.run(host='0.0.0.0', port=5000, debug=True)
