from typing import Literal

from pydantic import BaseModel, Field


FiscalDocumentType = Literal["unknown", "invoice", "delivery_note"]
FiscalDocumentStatus = Literal["processing", "ready", "error"]
FiscalMatchingStatus = Literal["pending", "not_started", "ready_for_match"]
FiscalReviewStatus = Literal["to_review", "reviewed"]
FiscalDocumentInboxSyncStatus = Literal["imported", "unsupported", "error"]


class FiscalDocumentRead(BaseModel):
    id: str
    original_name: str
    display_name: str
    mime_type: str
    kind: str
    file_size_bytes: int
    document_type: FiscalDocumentType
    document_number: str | None = None
    document_date: str | None = None
    supplier_name: str | None = None
    total_amount: float | None = None
    currency: str = "EUR"
    summary_text: str = ""
    extracted_text: str = ""
    status: FiscalDocumentStatus
    matching_status: FiscalMatchingStatus = "pending"
    review_status: FiscalReviewStatus = "to_review"
    error_detail: str | None = None
    created_at: str
    updated_at: str


class FiscalDocumentsListResponse(BaseModel):
    items: list[FiscalDocumentRead] = Field(default_factory=list)
    total_count: int = 0
    invoice_count: int = 0
    delivery_note_count: int = 0
    to_review_count: int = 0


class FiscalDocumentSettingsRead(BaseModel):
    inbound_email: str | None = None
    mailbox_configured: bool = False
    updated_at: str | None = None


class FiscalDocumentSettingsUpdatePayload(BaseModel):
    inbound_email: str | None = None


class FiscalDocumentInboxStatusRead(BaseModel):
    mailbox_configured: bool = False
    inbound_email: str | None = None
    google_configured: bool = False
    google_connected: bool = False
    google_account_email: str | None = None
    gmail_scope_granted: bool = False
    mailbox_matches_google_account: bool = False
    ready_for_sync: bool = False
    next_step: str


class FiscalDocumentInboxItemRead(BaseModel):
    id: str
    message_id: str
    attachment_id: str
    subject: str = ""
    sender: str | None = None
    received_at: str | None = None
    attachment_name: str
    mime_type: str
    sync_status: FiscalDocumentInboxSyncStatus
    document_id: str | None = None
    error_detail: str | None = None
    created_at: str
    updated_at: str


class FiscalDocumentInboxListResponse(BaseModel):
    items: list[FiscalDocumentInboxItemRead] = Field(default_factory=list)
    total_count: int = 0
    imported_count: int = 0
    unsupported_count: int = 0
    error_count: int = 0
