from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from typing import List

from app import models
from app.schemas.audit import FinancialAuditLogPublic
from app.database import get_session
from app.auth import current_active_user
from app.crud import audit as crud_audit, group as crud_group

router = APIRouter()

@router.get("/financial/group/{group_id}", response_model=List[FinancialAuditLogPublic])
async def read_financial_history_for_group(
    group_id: int,
    db: AsyncSession = Depends(get_session),
    current_user: models.User = Depends(current_active_user),
    skip: int = 0,
    limit: int = 100,
):
    """
    Retrieve financial audit history for a specific group.
    """
    is_member = await crud_group.is_user_member(db, group_id=group_id, user_id=current_user.id)
    if not is_member:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Not a member of this group")
        
    history = await crud_audit.get_financial_audit_logs_for_group(
        db=db, group_id=group_id, skip=skip, limit=limit
    )
    return history

@router.get("/financial/user/me", response_model=List[FinancialAuditLogPublic])
async def read_financial_history_for_user(
    db: AsyncSession = Depends(get_session),
    current_user: models.User = Depends(current_active_user),
    skip: int = 0,
    limit: int = 100,
):
    """
    Retrieve financial audit history for the current user.
    """
    history = await crud_audit.get_financial_audit_logs_for_user(
        db=db, user_id=current_user.id, skip=skip, limit=limit
    )
    return history 