from datetime import datetime
from typing import Optional
import uuid
from sqlalchemy import Column, DateTime, ForeignKey, String, Integer
from sqlmodel import Field, SQLModel
from utils.datetime_utils import get_current_utc_datetime


class JobModel(SQLModel, table=True):
    __tablename__ = "jobs"

    id: uuid.UUID = Field(primary_key=True, default_factory=uuid.uuid4)
    user_id: uuid.UUID = Field(sa_column=Column(ForeignKey("users.id")))
    client_id: uuid.UUID = Field(sa_column=Column(ForeignKey("clients.id")))
    presentation_id: Optional[uuid.UUID] = Field(
        sa_column=Column(ForeignKey("presentations.id"), nullable=True), default=None
    )
    job_type: str  # generate_presentation, parse_master_deck
    status: str = Field(default="queued")  # queued, processing, completed, failed
    progress: int = Field(sa_column=Column(Integer, default=0))
    progress_message: Optional[str] = Field(
        sa_column=Column(String, nullable=True), default=None
    )
    error_message: Optional[str] = Field(
        sa_column=Column(String, nullable=True), default=None
    )
    created_at: datetime = Field(
        sa_column=Column(
            DateTime(timezone=True), nullable=False, default=get_current_utc_datetime
        ),
    )
    started_at: Optional[datetime] = Field(
        sa_column=Column(DateTime(timezone=True), nullable=True), default=None
    )
    completed_at: Optional[datetime] = Field(
        sa_column=Column(DateTime(timezone=True), nullable=True), default=None
    )
