from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from app.api import routes_upload, routes_search
from app.db.models import Base
from app.db.session import engine

from sqlalchemy import text

with engine.begin() as conn:
    conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector;"))

Base.metadata.create_all(bind=engine)

from fastapi.middleware.cors import CORSMiddleware

app = FastAPI(title="Newspaper Extractor API")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allow all origins for development
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Serve uploaded images
import os
os.makedirs("/data/storage", exist_ok=True)
app.mount("/api/images", StaticFiles(directory="/data/storage"), name="images")

app.include_router(routes_upload.router)
app.include_router(routes_search.router)

@app.get("/")
def root():
    return {"message": "Newspaper Extractor API is running."}
