"""
文件管理相关的Pydantic Schema
"""
from typing import Optional, List, Dict, Any
from datetime import datetime
from pydantic import BaseModel, Field


# ===== 文件Schema =====

class UploadedFileBase(BaseModel):
    """上传文件基础Schema"""
    original_name: str = Field(..., min_length=1, max_length=255, description="原始文件名")
    file_size: int = Field(..., gt=0, description="文件大小(字节)")
    file_type: str = Field(..., description="文件类型(MIME)")
    remark: Optional[str] = Field(None, description="备注")


class UploadedFileCreate(UploadedFileBase):
    """创建文件记录Schema"""
    file_name: str = Field(..., description="存储文件名")
    file_path: str = Field(..., description="文件存储路径")
    file_ext: str = Field(..., description="文件扩展名")
    uploader_id: int = Field(..., gt=0, description="上传者ID")


class UploadedFileUpdate(BaseModel):
    """更新文件记录Schema"""
    remark: Optional[str] = None


class UploadedFileInDB(BaseModel):
    """数据库中的文件Schema"""
    id: int
    file_name: str
    original_name: str
    file_path: str
    file_size: int
    file_type: str
    file_ext: str
    uploader_id: int
    upload_time: datetime
    thumbnail_path: Optional[str]
    share_code: Optional[str]
    share_expire_time: Optional[datetime]
    download_count: int
    is_deleted: int
    deleted_at: Optional[datetime]
    deleted_by: Optional[int]
    remark: Optional[str]
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class UploadedFileResponse(UploadedFileInDB):
    """文件响应Schema"""
    uploader_name: Optional[str] = None


class UploadedFileWithUrl(UploadedFileResponse):
    """带访问URL的文件响应Schema"""
    download_url: Optional[str] = None
    preview_url: Optional[str] = None
    share_url: Optional[str] = None


# ===== 文件上传Schema =====

class FileUploadResponse(BaseModel):
    """文件上传响应Schema"""
    id: int
    file_name: str
    original_name: str
    file_size: int
    file_type: str
    file_path: str
    download_url: str
    preview_url: Optional[str] = None
    message: str = "上传成功"


# ===== 文件分享Schema =====

class FileShareCreate(BaseModel):
    """创建文件分享Schema"""
    expire_days: int = Field(default=7, ge=1, le=30, description="有效期(天)")


class FileShareResponse(BaseModel):
    """文件分享响应Schema"""
    share_code: str
    share_url: str
    expire_time: datetime


class FileShareVerify(BaseModel):
    """验证分享码Schema"""
    share_code: str = Field(..., description="分享码")


# ===== 批量操作Schema =====

class FileBatchDelete(BaseModel):
    """批量删除文件Schema"""
    file_ids: List[int] = Field(..., min_items=1, description="文件ID列表")


# ===== 查询参数Schema =====

class FileQueryParams(BaseModel):
    """文件查询参数"""
    keyword: Optional[str] = Field(None, description="搜索关键词")
    file_type: Optional[str] = Field(None, description="文件类型")
    uploader_id: Optional[int] = Field(None, gt=0, description="上传者ID")
    start_date: Optional[str] = Field(None, description="开始日期(YYYY-MM-DD)")
    end_date: Optional[str] = Field(None, description="结束日期(YYYY-MM-DD)")
    page: int = Field(default=1, ge=1, description="页码")
    page_size: int = Field(default=20, ge=1, le=100, description="每页数量")


# ===== 统计Schema =====

class FileStatistics(BaseModel):
    """文件统计Schema"""
    total_files: int = Field(..., description="总文件数")
    total_size: int = Field(..., description="总大小(字节)")
    total_size_human: str = Field(..., description="总大小(人类可读)")
    type_distribution: Dict[str, int] = Field(default_factory=dict, description="文件类型分布")
    upload_today: int = Field(..., description="今日上传数")
    upload_this_week: int = Field(..., description="本周上传数")
    upload_this_month: int = Field(..., description="本月上传数")
    top_uploaders: List[Dict[str, Any]] = Field(default_factory=list, description="上传排行")


# ===== 分片上传Schema =====

class ChunkUploadInit(BaseModel):
    """初始化分片上传Schema"""
    file_name: str = Field(..., description="文件名")
    file_size: int = Field(..., gt=0, description="文件大小")
    file_type: str = Field(..., description="文件类型")
    total_chunks: int = Field(..., gt=0, description="总分片数")
    file_hash: Optional[str] = Field(None, description="文件哈希(MD5/SHA256)")


class ChunkUploadInfo(BaseModel):
    """分片上传信息Schema"""
    upload_id: str = Field(..., description="上传ID")
    chunk_index: int = Field(..., ge=0, description="分片索引")


class ChunkUploadComplete(BaseModel):
    """完成分片上传Schema"""
    upload_id: str = Field(..., description="上传ID")
    file_name: str = Field(..., description="文件名")
    file_hash: Optional[str] = Field(None, description="文件哈希")
