from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel
from typing import Literal

from app.services.kis_client import kis_client

router = APIRouter()

class OrderRequest(BaseModel):
    market: Literal["Domestic", "Overseas"]
    side: Literal["buy", "sell"]
    code: str
    quantity: int
    price: float = 0 # 0 for Market Price (if supported)

@router.get("/price")
async def get_current_price(market: Literal["Domestic", "Overseas"], code: str):
    """
    Get Real-time Price (REST). Prefer WebSocket for streaming.
    """
    try:
        price = await kis_client.get_current_price(market, code)
        return {"code": code, "price": price}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/balance")
async def get_balance(market: Literal["Domestic", "Overseas"]):
    try:
        data = await kis_client.get_balance(market)
        return data
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.post("/order")
async def place_order(order: OrderRequest):
    try:
        res = await kis_client.place_order(order.market, order.side, order.code, order.quantity, order.price)
        return res
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
