import ulid from sqlalchemy.orm import Session from sqlalchemy import select from typing import Optional, List from fastapi import HTTPException, status from app.models.ServiceModel import ServiceJob, ServiceJobIntake, ServiceJobInspection, ServiceJobMedia, ServiceJobEvent from app.schemas.ServiceSchema import ServiceJobIntakeCreate, ServiceJobInspectionCreate class InspectionService: def create_device_intake( self, db: Session, job_id: str, staff_id: str, data: ServiceJobIntakeCreate ) -> ServiceJobIntake: job = db.get(ServiceJob, job_id) if not job: raise HTTPException(status_code=404, detail="Service job not found") # Create intake record intake = ServiceJobIntake( intake_id=str(ulid.ULID()), service_job_id=job_id, received_by=staff_id, power_status=data.power_status, screen_condition=data.screen_condition, body_condition=data.body_condition, back_condition=data.back_condition, camera_condition=data.camera_condition, accessories=data.accessories, customer_notes=data.customer_notes, technician_notes=data.technician_notes, ) db.add(intake) # Log Event & Update Job status job.status = "DEVICE_INTAKE" event = ServiceJobEvent( event_id=str(ulid.ULID()), job_id=job_id, event_type="DEVICE_INTAKE", performed_by=staff_id, notes="Device physical condition checked and logged during intake." ) db.add(event) db.commit() db.refresh(intake) return intake def submit_inspection( self, db: Session, job_id: str, technician_id: str, data: ServiceJobInspectionCreate ) -> ServiceJobInspection: job = db.get(ServiceJob, job_id) if not job: raise HTTPException(status_code=404, detail="Service job not found") # Create inspection record inspection = ServiceJobInspection( inspection_id=str(ulid.ULID()), service_job_id=job_id, technician_id=technician_id, result=data.result, customer_report=data.customer_report, confirmed_damage=data.confirmed_damage, additional_damage=data.additional_damage, notes=data.notes, ) db.add(inspection) # Log Event & Update Job status job.status = "INSPECTION_COMPLETED" event = ServiceJobEvent( event_id=str(ulid.ULID()), job_id=job_id, event_type="INSPECTION_COMPLETED", performed_by=technician_id, notes=f"Technician inspection complete. Result: {data.result}." ) db.add(event) db.commit() db.refresh(inspection) return inspection def upload_job_media( self, db: Session, job_id: str, category: str, file_id: str ) -> ServiceJobMedia: job = db.get(ServiceJob, job_id) if not job: raise HTTPException(status_code=404, detail="Service job not found") media = ServiceJobMedia( media_id=str(ulid.ULID()), service_job_id=job_id, category=category, file_id=file_id ) db.add(media) db.commit() db.refresh(media) return media def batch_upload_job_media( self, db: Session, job_id: str, category: str, file_ids: List[str] ) -> List[ServiceJobMedia]: job = db.get(ServiceJob, job_id) if not job: raise HTTPException(status_code=404, detail="Service job not found") created = [] for fid in file_ids: m = ServiceJobMedia( media_id=str(ulid.ULID()), service_job_id=job_id, category=category, file_id=fid ) db.add(m) created.append(m) db.commit() for item in created: db.refresh(item) return created def get_job_media( self, db: Session, job_id: str, category: Optional[str] = None ) -> List[dict]: from app.models.FileUploadModel import FileUpload query = db.query(ServiceJobMedia).filter(ServiceJobMedia.service_job_id == job_id) if category: query = query.filter(ServiceJobMedia.category == category) media_items = query.order_by(ServiceJobMedia.created_at.asc()).all() if not media_items: return [] file_ids = [m.file_id for m in media_items] files = db.query(FileUpload).filter(FileUpload.file_id.in_(file_ids)).all() file_map = {f.file_id: f for f in files} results = [] for media in media_items: file = file_map.get(media.file_id) url = (file.webp_path or file.raw_path or file.storage_path) if file else None results.append({ "media_id": media.media_id, "service_job_id": media.service_job_id, "category": media.category, "file_id": media.file_id, "created_at": media.created_at, "url": url, "webp_path": file.webp_path if file else None, "thumbnail_path": file.thumbnail_path if file else None, "mime_type": file.mime_type if file else None, "blur_hash": file.blur_hash if file else None, }) return results