# backend/app/api/v1/endpoints/expenses.py
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.api.deps import get_db, get_current_user
from app.models.asset import Asset, AssetCost # JAVÍTVA
from pydantic import BaseModel
from datetime import date

router = APIRouter()

class ExpenseCreate(BaseModel):
    asset_id: str
    category: str
    amount: float
    date: date

@router.post("/add")
async def add_expense(expense: ExpenseCreate, db: AsyncSession = Depends(get_db), current_user = Depends(get_current_user)):
    stmt = select(Asset).where(Asset.id == expense.asset_id)
    if not (await db.execute(stmt)).scalar_one_or_none():
        raise HTTPException(status_code=404, detail="Jármű nem található.")

    new_cost = AssetCost(
        asset_id=expense.asset_id,
        cost_type=expense.category,
        amount_local=expense.amount,
        date=expense.date,
        currency_local="HUF"
    )
    db.add(new_cost)
    await db.commit()
    return {"status": "success"}