from fastapi import APIRouter, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from process_query import process_query
from server_utils import limiter

router = APIRouter()
templates = Jinja2Templates(directory="templates")

@router.get("/{full_path:path}")
async def catch_all(request: Request, full_path: str):
    return templates.TemplateResponse(
        "github.jinja",
        {
            "request": request,
            "github_url": f"https://github.com/{full_path}",
            "loading": True,
            "default_file_size": 243
        }
    )

@router.post("/{full_path:path}", response_class=HTMLResponse)
@limiter.limit("10/minute") 
async def process_catch_all(
    request: Request, 
    input_text: str = Form(...),
    max_file_size: int = Form(...),
    pattern_type: str = Form(...),
    pattern: str = Form(...)
):
    return await process_query(request, input_text, max_file_size, pattern_type, pattern, is_index=False)
    