from fastapi import HTTPException
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import status

from src.models.company_user import User

from .service import Service
from src.models.company import Company



class CompanyService(Service):
    
    @staticmethod
    async def get_company_by_email(email:str,db:AsyncSession):
        stmt = select(Company).where(Company.email == email).limit(1)
        result = await db.execute(stmt)
        company = result.scalars().first()

        if not company:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="company not found"
            )
        return company
    
    @staticmethod
    async def get_company_by_id(company_id:str,db:AsyncSession):
        stmt = select(Company).where(Company.id == company_id).limit(1)
        result = await db.execute(stmt)
        company = result.scalars().first()

        if not company:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="company not found"
            )
        return company
    @staticmethod
    async def get_companyuser_by_email(email: str, db: AsyncSession) -> User:
        stmt = select(User).where(User.email == email).limit(1)
        result = await db.execute(stmt)
        user = result.scalars().first()
        if not user:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="User not found"
            )
        return user

    @staticmethod
    async def get_companyuser_by_id(user_id:str,db:AsyncSession) -> User:
        stmt = select(User).where(User.id == user_id).limit(1)
        result = await db.execute(stmt)
        user = result.scalars().first()

        if not user:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="User not found"
            )
        return user
