from typing import List, Optional
from sqlmodel import Session, select
from app.core.base_repository import BaseRepository
from app.models.UmbrellaInsuranceModel import UmbrellaInsuranceModel
from app.schemas.UmbrellaInsuranceSchema import UmbrellaInsuranceCreate, UmbrellaInsuranceUpdate
from app.utils.transaction_generator import generate_transaction_number

class UmbrellaInsuranceRepository(BaseRepository[UmbrellaInsuranceModel, UmbrellaInsuranceCreate, UmbrellaInsuranceUpdate]):
    def __init__(self, session: Session):
        super().__init__(UmbrellaInsuranceModel, session)

    def create(self, obj_in: UmbrellaInsuranceCreate) -> UmbrellaInsuranceModel:
        obj_data = obj_in.model_dump()
        obj_data['transaction_no'] = generate_transaction_number('umbrella')
        
        # Encrypt sensitive fields
        obj_data = self._encrypt_fields(obj_data)
        
        db_obj = self.model(**obj_data)
        return super().create_db_object(db_obj)

    def update(self, db_obj: UmbrellaInsuranceModel, obj_in: UmbrellaInsuranceUpdate) -> UmbrellaInsuranceModel:
        obj_data = obj_in.model_dump(exclude_unset=True)
        return super().update(db_obj, obj_data)

    def get_with_decryption(self, id: int) -> Optional[UmbrellaInsuranceModel]:
        return super().get(id)

    def get_multi_with_decryption(self, skip: int = 0, limit: int = 100) -> List[UmbrellaInsuranceModel]:
        return super().get_multi(skip=skip, limit=limit)

    def search_by_name(self, name: str) -> List[UmbrellaInsuranceModel]:
        return self.search_by_field("full_name", name)

    def search_by_email(self, email: str) -> List[UmbrellaInsuranceModel]:
        return self.search_by_field("email_address", email)

    def get_by_state(self, state: str) -> List[UmbrellaInsuranceModel]:
        stmt = select(self.model).where(self.model.state_of_residence == state)
        return self.session.exec(stmt).all()

    def get_by_status(self, status: str) -> List[UmbrellaInsuranceModel]:
        stmt = select(self.model).where(self.model.quote_status == status)
        return self.session.exec(stmt).all()