from datetime import datetime
from typing import Literal, Optional

from pydantic import BaseModel, ConfigDict, Field
from pydantic.alias_generators import to_camel

from persona_models.common import create_uuid

TriggerSourceType = Literal["webhook", "schedule", "gmail", "imap"]
TriggerDestinationType = Literal["workflow"]


class TriggerSource(BaseModel):
    type: TriggerSourceType
    configuration: Optional[dict] = None

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)


class TriggerDestination(BaseModel):
    type: TriggerDestinationType
    configuration: Optional[dict] = None

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)


class Trigger(BaseModel):
    id: str = Field(default_factory=create_uuid)
    project_id: str | None = None
    name: str
    source: TriggerSource
    destination: TriggerDestination
    enabled: bool = True
    created_at: datetime = Field(default_factory=datetime.now)
    processed_at: Optional[datetime] = None

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)


class TriggerExecutionResult(BaseModel):
    id: str = Field(default_factory=create_uuid)
    trigger_id: str
    success: bool
    message: Optional[str] = None
    processed_at: datetime = Field(default_factory=datetime.now)

    model_config = ConfigDict(populate_by_name=True, alias_generator=to_camel)
