import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from app import create_app
from config import Config

# Versuche PostgreSQL-Dienst zu starten falls nicht aktiv
try:
    svc = __import__('subprocess', fromlist=['check_call']).check_call(
        ['powershell', '-Command', 'if ((Get-Service postgresql-x64-16 -ErrorAction SilentlyContinue).Status -ne "Running") { Start-Process -FilePath "powershell" -ArgumentList "Start-Service postgresql-x64-16" -Verb RunAs -Wait }'],
        stdout=__import__('subprocess', fromlist=['DEVNULL']).DEVNULL,
        stderr=__import__('subprocess', fromlist=['DEVNULL']).DEVNULL
    )
except Exception:
    pass

app = create_app()

if __name__ == '__main__':
    is_pg = 'postgresql' in Config.SQLALCHEMY_DATABASE_URI
    print("=" * 60)
    print("  AufmassWeb – Starte Server...")
    print(f"  DB: {'PostgreSQL' if is_pg else 'SQLite'} [{Config.SQLALCHEMY_DATABASE_URI}]")
    print("=" * 60)
    print("  Oeffne im Browser: http://localhost:5000")
    print("=" * 60)
    app.run(host='0.0.0.0', port=5000, debug=True)
