from __future__ import annotations

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field


class ReceiptFolderFieldRead(BaseModel):
    key: str = ""
    label: str = ""
    value: str = ""


class ReceiptFolderFieldChangeRead(BaseModel):
    key: str = ""
    label: str = ""
    before: str = ""
    after: str = ""


class ReceiptFolderEditLogRead(BaseModel):
    operated_at: datetime | None = None
    operator: str = ""
    changes: list[ReceiptFolderFieldChangeRead] = Field(default_factory=list)


class ReceiptFolderItemRead(BaseModel):
    id: str
    file_name: str
    media_type: str = "application/octet-stream"
    size_bytes: int = 0
    status: str = "unlinked"
    status_label: str = "未关联"
    document_type: str = "other"
    document_type_label: str = "其他单据"
    scene_code: str = "other"
    scene_label: str = "其他票据"
    summary: str = ""
    amount: str = ""
    document_date: str = ""
    merchant_name: str = ""
    avg_score: float = 0.0
    uploaded_at: datetime | None = None
    linked_at: datetime | None = None
    linked_claim_id: str = ""
    linked_claim_no: str = ""
    previewable: bool = False
    preview_kind: str = ""
    preview_url: str = ""
    source_url: str = ""
    warnings: list[str] = Field(default_factory=list)


class ReceiptFolderDetailRead(ReceiptFolderItemRead):
    engine: str = ""
    model: str = ""
    ocr_text: str = ""
    line_count: int = 0
    page_count: int = 1
    classification_confidence: float = 0.0
    classification_evidence: list[str] = Field(default_factory=list)
    fields: list[ReceiptFolderFieldRead] = Field(default_factory=list)
    edit_logs: list[ReceiptFolderEditLogRead] = Field(default_factory=list)
    raw_meta: dict[str, Any] = Field(default_factory=dict)


class ReceiptFolderUpdate(BaseModel):
    document_type: str | None = None
    document_type_label: str | None = None
    scene_code: str | None = None
    scene_label: str | None = None
    summary: str | None = None
    amount: str | None = None
    document_date: str | None = None
    merchant_name: str | None = None
    fields: list[ReceiptFolderFieldRead] | None = None


class ReceiptFolderDeleteResponse(BaseModel):
    message: str
    receipt_id: str
