#!/usr/bin/env python3
"""
Simplified startup script for AI Text Processing system.
"""

import os
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Create FastAPI app
app = FastAPI(title="Humanize App", description="AI Text Processing API")

# Configuration
class Config:
    MAX_WORKERS = os.cpu_count() * 2 + 1
    KEEPALIVE_TIMEOUT = 65
    MAX_CONNECTIONS = 1000

config = Config()

# Define a simple API endpoint
@app.get("/api/health")
async def health_check():
    return {"status": "healthy", "service": "Humanize App"}

# Define a simple text processing endpoint
@app.post("/api/process")
async def process_text(request: Request):
    data = await request.json()
    text = data.get("text", "")
    
    # Simple text processing logic
    processed_text = text.replace("bad", "good").replace("hate", "love")
    
    return {
        "original": text,
        "processed": processed_text,
        "status": "success"
    }

# Serve the HTML file
@app.get("/", response_class=HTMLResponse)
async def serve_html():
    with open("updated_frontend (1).html", "r") as f:
        html_content = f.read()
    return HTMLResponse(content=html_content)

if __name__ == "__main__":
    # Production configuration
    port = int(os.environ.get("PORT", 8000))
    # Determine number of workers
    if os.environ.get("WEB_CONCURRENCY"):
        workers = int(os.environ.get("WEB_CONCURRENCY"))
    else:
        workers = config.MAX_WORKERS

    logger.info(f"Starting server on port {port} with {workers} workers")
    
    uvicorn.run(
        "sorry:app",
        host="0.0.0.0",
        port=port,
        workers=1,  # Simplified to 1 worker for testing
        reload=True,  # Enable reload for development
        log_level="info"
    )
