#!/usr/bin/env python3
"""
Sharey - Production Entry Point
Simple Python-based entry point for the Sharey file sharing platform
"""

import sys
import os
import subprocess
import argparse
from pathlib import Path

def main():
    parser = argparse.ArgumentParser(description='Sharey File Sharing Platform')
    parser.add_argument('command', nargs='?', default='start', 
                       choices=['start', 'stop', 'restart', 'status'],
                       help='Command to execute (default: start)')
    parser.add_argument('--port', '-p', type=int, default=8000,
                       help='Port to run on (default: 8000)')
    parser.add_argument('--host', default='0.0.0.0',
                       help='Host to bind to (default: 0.0.0.0)')
    
    args = parser.parse_args()
    
    script_dir = Path(__file__).parent
    os.chdir(script_dir)
    
    if args.command == 'start':
        print("🚀 Starting Sharey production server...")
        subprocess.run([sys.executable, 'production.py'], check=True)
    
    elif args.command == 'stop':
        print("🛑 Stopping Sharey server...")
        # Kill gunicorn processes
        try:
            subprocess.run(['pkill', '-f', 'gunicorn.*sharey'], check=False)
            print("✅ Server stopped")
        except:
            print("❌ Error stopping server")
    
    elif args.command == 'restart':
        print("🔄 Restarting Sharey server...")
        subprocess.run(['pkill', '-f', 'gunicorn.*sharey'], check=False)
        subprocess.run([sys.executable, 'production.py'], check=True)
    
    elif args.command == 'status':
        print("📊 Checking Sharey server status...")
        result = subprocess.run(['pgrep', '-f', 'gunicorn.*sharey'], 
                              capture_output=True, text=True)
        if result.stdout.strip():
            print("✅ Server is running")
            print(f"PIDs: {result.stdout.strip()}")
        else:
            print("❌ Server is not running")

if __name__ == '__main__':
    main()
