"""
Generation Job Model
Tracking für Content-Generierungs-Jobs
"""

from enum import Enum
from datetime import datetime
from typing import Optional, Dict, Any
import uuid


class JobStatus(str, Enum):
    """Job Status"""
    PENDING = "pending"
    PROCESSING = "processing"
    COMPLETED = "completed"
    FAILED = "failed"


class GenerationJob:
    """Content-Generierungs-Job"""

    def __init__(
        self,
        topic: str,
        description: Optional[str] = None,
        target_grade: Optional[str] = None,
        material_count: int = 0
    ):
        self.job_id = str(uuid.uuid4())
        self.topic = topic
        self.description = description
        self.target_grade = target_grade
        self.material_count = material_count

        self.status = JobStatus.PENDING
        self.progress = 0
        self.message = "Job created"
        self.result: Optional[Dict[str, Any]] = None
        self.error: Optional[str] = None

        self.created_at = datetime.utcnow()
        self.updated_at = datetime.utcnow()

    def update_progress(self, progress: int, message: str):
        """Update Job Progress"""
        self.progress = progress
        self.message = message
        self.status = JobStatus.PROCESSING
        self.updated_at = datetime.utcnow()

    def complete(self, result: Dict[str, Any]):
        """Mark Job as Completed"""
        self.status = JobStatus.COMPLETED
        self.progress = 100
        self.message = "Content generation completed"
        self.result = result
        self.updated_at = datetime.utcnow()

    def fail(self, error: str):
        """Mark Job as Failed"""
        self.status = JobStatus.FAILED
        self.message = "Content generation failed"
        self.error = error
        self.updated_at = datetime.utcnow()

    def to_dict(self) -> Dict[str, Any]:
        """Convert to dict"""
        return {
            "job_id": self.job_id,
            "topic": self.topic,
            "description": self.description,
            "target_grade": self.target_grade,
            "material_count": self.material_count,
            "status": self.status.value,
            "progress": self.progress,
            "message": self.message,
            "result": self.result,
            "error": self.error,
            "created_at": self.created_at.isoformat(),
            "updated_at": self.updated_at.isoformat()
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> "GenerationJob":
        """Create from dict"""
        job = cls(
            topic=data["topic"],
            description=data.get("description"),
            target_grade=data.get("target_grade"),
            material_count=data.get("material_count", 0)
        )
        job.job_id = data["job_id"]
        job.status = JobStatus(data["status"])
        job.progress = data["progress"]
        job.message = data["message"]
        job.result = data.get("result")
        job.error = data.get("error")
        job.created_at = datetime.fromisoformat(data["created_at"])
        job.updated_at = datetime.fromisoformat(data["updated_at"])
        return job
