"""
标注数据导出相关 Schema
"""

from __future__ import annotations

from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field


class ExportFormat(str, Enum):
    """导出格式枚举"""
    JSON = "json"           # Label Studio 原生 JSON 格式
    JSONL = "jsonl"         # JSON Lines 格式（每行一条记录）
    CSV = "csv"             # CSV 表格格式
    COCO = "coco"           # COCO 目标检测格式
    YOLO = "yolo"           # YOLO 格式


class ExportAnnotationsRequest(BaseModel):
    """导出标注数据请求"""
    format: ExportFormat = Field(default=ExportFormat.JSON, description="导出格式")
    include_data: bool = Field(default=True, description="是否包含原始数据（如文本内容）")
    only_annotated: bool = Field(default=True, description="是否只导出已标注的数据")

    model_config = {"use_enum_values": True}


class ExportAnnotationsResponse(BaseModel):
    """导出标注数据响应（用于预览/统计）"""
    project_id: str = Field(..., description="项目ID")
    project_name: str = Field(..., description="项目名称")
    total_files: int = Field(..., description="总文件数")
    annotated_files: int = Field(..., description="已标注文件数")
    export_format: str = Field(..., description="导出格式")

    model_config = {"populate_by_name": True}


class AnnotationExportItem(BaseModel):
    """单条导出记录"""
    file_id: str = Field(..., description="文件ID")
    file_name: str = Field(..., description="文件名")
    data: Optional[Dict[str, Any]] = Field(default=None, description="原始数据")
    annotations: List[Dict[str, Any]] = Field(default_factory=list, description="标注结果")
    created_at: Optional[datetime] = Field(default=None, description="创建时间")
    updated_at: Optional[datetime] = Field(default=None, description="更新时间")


class COCOExportFormat(BaseModel):
    """COCO 格式导出结构"""
    info: Dict[str, Any] = Field(default_factory=dict)
    licenses: List[Dict[str, Any]] = Field(default_factory=list)
    images: List[Dict[str, Any]] = Field(default_factory=list)
    annotations: List[Dict[str, Any]] = Field(default_factory=list)
    categories: List[Dict[str, Any]] = Field(default_factory=list)
