from sqlalchemy.orm import Session from sqlalchemy import select from typing import List, Optional from app.repositories.base_repository import BaseRepository from app.models.ServiceModel import ServiceJob, ServiceCatalog, ServiceAppointment, ServiceJobAssignment from app.models.CustomerDeviceModel import CustomerDevice class ServiceRepository(BaseRepository[ServiceJob]): def __init__(self): super().__init__(ServiceJob) def get_by_job_no(self, db: Session, job_no: str) -> Optional[ServiceJob]: stmt = select(ServiceJob).where(ServiceJob.job_no == job_no) return db.execute(stmt).scalar_one_or_none() def get_customer_jobs(self, db: Session, customer_id: str) -> List[ServiceJob]: stmt = select(ServiceJob).where(ServiceJob.customer_id == customer_id).order_by(ServiceJob.created_at.desc()) return list(db.execute(stmt).scalars().all()) def get_catalog_service(self, db: Session, service_id: str) -> Optional[ServiceCatalog]: return db.get(ServiceCatalog, service_id) def get_all_catalog_services(self, db: Session, active_only: bool = True) -> List[ServiceCatalog]: stmt = select(ServiceCatalog) if active_only: stmt = stmt.where(ServiceCatalog.active == True) items = list(db.execute(stmt).scalars().all()) if not items: from app.models.DeviceCatalogModel import ServiceType st_stmt = select(ServiceType) if active_only: st_stmt = st_stmt.where(ServiceType.is_active == True) service_types = list(db.execute(st_stmt).scalars().all()) return [ ServiceCatalog( service_id=st.service_type_id, name=st.name, description=st.description or f"{st.name} service", base_price=999.00, estimated_duration_minutes=60, workflow_type="REPAIR_QUOTE", active=True ) for st in service_types ] return items def get_appointment(self, db: Session, appointment_id: str) -> Optional[ServiceAppointment]: return db.get(ServiceAppointment, appointment_id) def get_active_appointments_in_range(self, db: Session, start_time, end_time) -> List[ServiceAppointment]: stmt = select(ServiceAppointment).where( ServiceAppointment.scheduled_start < end_time, ServiceAppointment.scheduled_end > start_time, ServiceAppointment.status.notin_(["CANCELLED", "EXPIRED"]) ) return list(db.execute(stmt).scalars().all()) def get_customer_devices(self, db: Session, customer_id: str) -> List[CustomerDevice]: stmt = select(CustomerDevice).where(CustomerDevice.customer_id == customer_id) return list(db.execute(stmt).scalars().all())