"""BatchUpload and BatchJob SQLModel models for bulk receipt processing."""

from datetime import datetime
from enum import Enum
from typing import Optional

from sqlmodel import SQLModel, Field


class BatchStatus(str, Enum):
    """Status of a batch upload."""
    PENDING = "pending"          # Batch created, jobs queued
    PROCESSING = "processing"    # At least one job is processing
    COMPLETED = "completed"      # All jobs completed (success or failed)
    FAILED = "failed"            # Batch-level failure (e.g., all jobs failed)


class BatchUpload(SQLModel, table=True):
    """
    Batch upload record for grouping multiple OCR jobs.

    Tracks overall progress and status of a bulk upload operation.
    """

    __tablename__ = "batch_uploads"

    id: Optional[int] = Field(default=None, primary_key=True)

    # User info
    user_id: str = Field(max_length=100, index=True)  # Username who created the batch
    company_id: int = Field(index=True)  # Company ID for receipt creation

    # Timestamps
    created_at: datetime = Field(default_factory=datetime.utcnow)

    # Status tracking
    status: BatchStatus = Field(default=BatchStatus.PENDING)
    total_files: int = Field(default=0)


class BatchJob(SQLModel, table=True):
    """
    Junction table linking batch_uploads to ocr_jobs.

    Each record represents one file in a batch, linking to its OCR job.
    Also stores the receipt_id once the job completes and auto-creates a receipt.
    """

    __tablename__ = "batch_jobs"

    id: Optional[int] = Field(default=None, primary_key=True)

    # Foreign keys
    batch_id: int = Field(foreign_key="batch_uploads.id", index=True)
    job_id: str = Field(max_length=36, index=True)  # UUID from ocr_jobs table

    # Original filename for display
    filename: str = Field(max_length=255)

    # Receipt reference (set after auto-create)
    receipt_id: Optional[int] = Field(default=None, foreign_key="receipts.id")

    # Timestamps
    created_at: datetime = Field(default_factory=datetime.utcnow)
