"""
Tool_OCR - Export Schemas
"""

from datetime import datetime
from typing import Optional, Dict, Any, List
from pydantic import BaseModel, Field


class ExportOptions(BaseModel):
    """Export options schema"""
    confidence_threshold: Optional[float] = Field(None, description="Minimum confidence threshold")
    include_metadata: Optional[bool] = Field(True, description="Include metadata in export")
    filename_pattern: Optional[str] = Field(None, description="Filename pattern for export")
    css_template: Optional[str] = Field(None, description="CSS template for PDF export")


class ExportRequest(BaseModel):
    """Export request schema"""
    batch_id: int = Field(..., description="Batch ID to export")
    format: str = Field(..., description="Export format (txt, json, excel, markdown, pdf, zip)")
    rule_id: Optional[int] = Field(None, description="Optional export rule ID to apply")
    css_template: Optional[str] = Field("default", description="CSS template for PDF export")
    include_formats: Optional[List[str]] = Field(None, description="Formats to include in ZIP export")
    options: Optional[ExportOptions] = Field(None, description="Additional export options")

    class Config:
        json_schema_extra = {
            "example": {
                "batch_id": 1,
                "format": "pdf",
                "rule_id": None,
                "css_template": "default",
                "include_formats": ["markdown", "json"],
                "options": {
                    "confidence_threshold": 0.8,
                    "include_metadata": True
                }
            }
        }


class ExportRuleCreate(BaseModel):
    """Export rule creation schema"""
    rule_name: str = Field(..., max_length=100, description="Rule name")
    description: Optional[str] = Field(None, description="Rule description")
    config_json: Dict[str, Any] = Field(..., description="Rule configuration as JSON")
    css_template: Optional[str] = Field(None, description="Custom CSS template")

    class Config:
        json_schema_extra = {
            "example": {
                "rule_name": "High Confidence Only",
                "description": "Export only results with confidence > 0.8",
                "config_json": {
                    "filters": {
                        "confidence_threshold": 0.8
                    },
                    "formatting": {
                        "add_line_numbers": True
                    }
                },
                "css_template": None
            }
        }


class ExportRuleUpdate(BaseModel):
    """Export rule update schema"""
    rule_name: Optional[str] = Field(None, max_length=100)
    description: Optional[str] = None
    config_json: Optional[Dict[str, Any]] = None
    css_template: Optional[str] = None


class ExportRuleResponse(BaseModel):
    """Export rule response schema"""
    id: int
    user_id: int
    rule_name: str
    description: Optional[str] = None
    config_json: Dict[str, Any]
    css_template: Optional[str] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class CSSTemplateResponse(BaseModel):
    """CSS template response schema"""
    name: str = Field(..., description="Template name")
    description: str = Field(..., description="Template description")
    filename: str = Field(..., description="Template filename")

    class Config:
        json_schema_extra = {
            "example": {
                "name": "default",
                "description": "通用排版模板，適合大多數文檔",
                "filename": "default.css"
            }
        }
