# services/comfy-agent/app/jobs.py
import uuid
from typing import Dict, Optional
from .models import JobStatus, GenType

class JobStore:
    def __init__(self) -> None:
        self._jobs: Dict[str, JobStatus] = {}

    @staticmethod
    def new_job_id() -> str:
        return f"job_{uuid.uuid4().hex}"

    def create(self, gen_type: GenType, job_id: Optional[str] = None) -> JobStatus:
        job_id = job_id or self.new_job_id()
        js = JobStatus(job_id=job_id, type=gen_type, status="queued", progress=0.0)
        self._jobs[job_id] = js
        return js

    def get(self, job_id: str) -> Optional[JobStatus]:
        return self._jobs.get(job_id)

    def update(self, job_id: str, **patch) -> JobStatus:
        js = self._jobs[job_id]
        updated = js.model_copy(update=patch)
        self._jobs[job_id] = updated
        return updated

JOB_STORE = JobStore()
