from typing import Annotated from fastapi import APIRouter, Depends from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.api.deps import get_current_active_user from app.core.admin_database import get_admin_db from app.core.config import settings from app.models.account import Account from app.models.user import User from app.schemas.billing import CheckoutSessionCreate, CheckoutSessionResponse from app.services.billing import BillingService router = APIRouter(prefix="/billing", tags=["billing"]) @router.post("/checkout-session", response_model=CheckoutSessionResponse) async def create_checkout_session( payload: CheckoutSessionCreate, current_user: Annotated[User, Depends(get_current_active_user)], db: Annotated[AsyncSession, Depends(get_admin_db)], ) -> CheckoutSessionResponse: account = (await db.execute( select(Account).where(Account.id == current_user.account_id) )).scalar_one() url = await BillingService.create_checkout_session( db=db, account=account, plan=payload.plan, seats=payload.seats, billing_interval=payload.billing_interval, success_url=f"{settings.FRONTEND_URL}/account/billing?success=1", cancel_url=f"{settings.FRONTEND_URL}/account/billing/select-plan", ) return CheckoutSessionResponse(url=url)