from fastapi import APIRouter, Depends, HTTPException, Request, UploadFile, File
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from typing import Optional, List
from datetime import datetime, timezone
from pydantic import BaseModel, Field
from typing import Optional, Any, Union




class BatchEnterRequest(BaseModel):
    ids: list[Union[int, str]]
    table_type: Optional[str] = None
    kb_method: Optional[str] = None
    chunk_size: Optional[int] = Field(500, description="切分长度")
    separator: Optional[str] = Field("。", description="切分符号")

class BatchDeleteRequest(BaseModel):
    ids: list[Union[int, str]]
    table_type: Optional[str] = None

class ConvertRequest(BaseModel):
    id: Union[int, str]
    table_type: Optional[str] = None



class DocumentAdd(BaseModel):
    title: str
    note: Optional[str] = None
    primary_category_id: Optional[Any] = None
    secondary_category_id: Optional[Any] = None
    year: Optional[int] = None
    table_type: Optional[str] = "standard"
    # 新增编辑需要的字段
    id: Optional[str] = None
    # 扩展字段 (子表特有属性)
    standard_no: Optional[str] = None
    issuing_authority: Optional[str] = None
    release_date: Optional[str] = None
    document_type: Optional[str] = None
    professional_field: Optional[str] = None
    validity: Optional[str] = None
    project_name: Optional[str] = None
    project_section: Optional[str] = None
    compilation_basis: Optional[str] = None
    plan_summary: Optional[str] = None
    plan_category: Optional[str] = None
    level_1_classification: Optional[str] = "施工方案"
    level_2_classification: Optional[str] = None
    level_3_classification: Optional[str] = None
    level_4_classification: Optional[str] = None
    # 更多基础信息字段
    english_name: Optional[str] = None
    implementation_date: Optional[str] = None
    drafting_unit: Optional[str] = None
    approving_department: Optional[str] = None
    engineering_phase: Optional[str] = None
    participating_units: Optional[str] = None
    reference_basis: Optional[str] = None
    source_url: Optional[str] = None
    effective_start_date: Optional[str] = None
    effective_end_date: Optional[str] = None
    # 文件相关字段
    file_url: Optional[str] = None
    json_url: Optional[str] = None
    file_extension: Optional[str] = None
    kb_id: str
 
class DocumentListRequest(BaseModel):
    page: int = 1
    size: int = 50
    keyword: Optional[str] = None
    table_type: Optional[str] = None
    whether_to_enter: Optional[int] = None

class UploadUrlRequest(BaseModel):
    filename: str
    content_type: str
    prefix: Optional[str] = None

class UploadUrlResponse(BaseModel):
    upload_url: str
    file_url: str
    object_name: str

class ExportRequest(BaseModel):
    project_id: Optional[str] = None
    format: Optional[str] = "json"
    completed_only: Optional[bool] = False