"""Job Debug Logs."""

from datetime import datetime
from enum import Enum
from typing import Any
from uuid import UUID

import pytz
from msgspec import Struct, field

from slices_clientlib_aiis.schema.job import JobStatus
from slices_clientlib_aiis.util.msgspec import AwareDatetime


class JobDebugLogType(Enum):
    """Job Event Type."""

    STATUS_CHANGE = "Status Change"
    DEBUG = "Debug"
    INFO = "Info"
    WARN = "Warning"
    ERROR = "Error"

    @classmethod
    def _missing_(cls, value: Any):
        """Handle missing enum values by ignoring case."""
        if isinstance(value, cls):
            return value
        if not isinstance(value, str):
            return None
        for member in cls:
            if member.name.lower() == value.lower():
                return member
            if member.value.lower() == value.lower():
                return member
        return None


class JobDebugLog(Struct, rename="camel"):
    """Job Debug Log aka Job Event."""

    job_id: UUID
    type: JobDebugLogType
    time: AwareDatetime | None = field(default_factory=lambda: datetime.now(tz=pytz.utc))
    new_state: JobStatus | None = None
    msg: str | None = None
