from sqlalchemy.ext.asyncio import AsyncSession

from src.db.models.form_type import FormTypeEnum
from src.db.models.role import UserRoleEnum
from src.db.models.app_user import AppUser
from src.db.models.expense_item import ExpenseItem
from src.repository.expense_item_repository import ExpenseItemRepository


class ExpenseItemService:
    def __init__(self, db: AsyncSession):
        self.repo = ExpenseItemRepository(db)

    async def get(
            self, 
            item_id: int, 
            user: AppUser | None = None,
    ) -> ExpenseItem | None:
        if not user or user.role_id == UserRoleEnum.ADMIN:
            return await self.repo.get(item_id=item_id)
        return None

    async def get_list(
            self, 
            r_start: bool | None = None,
            sheet: str | None = None,
            direction: str | None = None,
            form_type: FormTypeEnum | str | None = None,
    ) -> list[ExpenseItem]:
        return await self.repo.get_list(
            r_start=r_start,
            sheet=sheet,
            direction=direction,
            form_type=form_type,
        )
