from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from app.api.deps import get_current_user
from app.db.session import get_db
from app.schemas.payment import PaymentCreate, PaymentResponse, PaymentUpdate
from app.schemas.user_context import CurrentUser
from app.services.policy_service import PolicyService

router = APIRouter()


@router.get("/{policy_id}/payments", response_model=list[PaymentResponse])
def list_payments(
    policy_id: int,
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return PolicyService(db).list_payments(current_user.agency_id, policy_id)


@router.post("/{policy_id}/payments", response_model=PaymentResponse)
def add_payment(
    policy_id: int,
    payload: PaymentCreate,
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return PolicyService(db).add_payment(current_user.agency_id, policy_id, current_user.id, payload)


@router.put("/{policy_id}/payments/{payment_id}", response_model=PaymentResponse)
def update_payment(
    policy_id: int,
    payment_id: int,
    payload: PaymentUpdate,
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    return PolicyService(db).update_payment(current_user.agency_id, policy_id, payment_id, payload)


@router.delete("/{policy_id}/payments/{payment_id}")
def delete_payment(
    policy_id: int,
    payment_id: int,
    current_user: CurrentUser = Depends(get_current_user),
    db: Session = Depends(get_db),
):
    PolicyService(db).delete_payment(current_user.agency_id, policy_id, payment_id)
    return {"message": "Payment deleted successfully"}
