from fastapi import APIRouter
from psycopg2.extras import RealDictCursor
from database import get_db_connection   

router = APIRouter()

@router.get("/users")
async def get_users():
    conn = get_db_connection()
    cursor = conn.cursor(cursor_factory=RealDictCursor)
    cursor.execute("SELECT * FROM users")
    rows = cursor.fetchall()
    conn.close()
    return rows   

@router.post("/users")
async def add_user(name: str, email: str):
    conn = get_db_connection()
    cursor = conn.cursor()
    cursor.execute("INSERT INTO users (name, email) VALUES (%s, %s) ON CONFLICT (email) DO NOTHING", (name, email))
    conn.commit()
    conn.close()
    return {"status": "success"}   