
from sqlalchemy.ext.asyncio import AsyncSession

from src.db.models.app_user import AppUser
from src.db.models.budget_form import BudgetForm
from src.repository.sheet_repository import SheetRepository



class SheetService:
    def __init__(self, db: AsyncSession):
        self.db = db
        self.sheet_repo = SheetRepository(db)

    async def get(
            self, 
            sheet: str, 
            direction: str | None, 
            sections: list[str] | None = None,
            form_id: int | None = None,
            form: BudgetForm | None = None
    ) -> list[tuple]:        
        return await self.sheet_repo.get(
            form_id=form_id,
            sheet=sheet,
            direction=direction,
            sections=sections,
        )
    
    async def update_cell(
            self, 
            form_id: int, 
            sheet: str,
            direction: str | None,
            sections: list[str] | None,
            line_id: int, 
            column: str, 
            value: str | int | float | None = None, 
            user: AppUser | None = None,
    ) -> list[tuple]:
        return await self.sheet_repo.update_cell(
            form_id=form_id, 
            sheet=sheet,
            direction=direction,
            sections=sections,
            line_id=line_id, 
            column=column, 
            value=value, 
            user_id=user.id if user else None,
        )

    async def update_cells(
            self, 
            form_id: int, 
            sheet: str,
            direction: str | None,
            sections: list[str] | None,
            changes: list[dict],
            user: AppUser | None = None,
    ) -> list[tuple]:
        return await self.sheet_repo.update_cells(
            form_id=form_id, 
            sheet=sheet,
            direction=direction,
            sections=sections,
            changes=changes,
            user_id=user.id if user else None,
        )
    
    async def update_cell(
            self, 
            form_id: int, 
            sheet: str,
            direction: str | None,
            sections: list[str] | None,
            line_id: int, 
            column: str, 
            value: str | int | float | None = None, 
            user: AppUser | None = None,
    ) -> list[tuple]:
        return await self.sheet_repo.update_cell(
            form_id=form_id, 
            sheet=sheet,
            direction=direction,
            sections=sections,
            line_id=line_id, 
            column=column, 
            value=value, 
            user_id=user.id if user else None,
        )


    async def add_line(
            self, 
            form_id: int,
            sheet: str,
            expense_item_id: int | None = None,
            item_id: str | None = None,
            section_code: str | None = None,
            direction: str | None = None,
            name: str | None = None,
            internal_order: str | None = None,
            vsp_id: int | None = None,
            project_id: int | None = None,
            justification: str | None = None,
            contract_number: str | None = None,
            contract_end_date: str | None = None,
            user: AppUser | None = None,
    ) -> list[tuple]:
        return await self.sheet_repo.add_line(
            form_id=form_id,
            sheet=sheet,
            expense_item_id=expense_item_id,
            item_id=item_id,
            section_code=section_code,
            direction=direction,
            name=name,
            internal_order=internal_order,
            vsp_id=vsp_id,
            project_id=project_id,
            justification=justification,
            contract_number=contract_number,
            contract_end_date=contract_end_date,
            user_id=user.id if user else None,
        )

    async def delete_line(
            self,
            form_id: int,
            sheet: str,
            row_id: int,
            direction: str | None = None,
            user: AppUser | None = None,
    ) -> list[tuple]:
        return await self.sheet_repo.delete_line(
            sheet=sheet,
            row_id=row_id,
            direction=direction,
        )