from datetime import datetime

from pydantic import BaseModel, Field


class ChunkedUploadInitRequest(BaseModel):
    filename: str = Field(..., min_length=1, max_length=255, description="Filename")
    file_size: int = Field(..., gt=0, le=2147483648, description="File size in bytes, max 2GB")
    total_chunks: int = Field(..., gt=0, description="Total number of chunks")
    content_type: str = Field(
        default="application/octet-stream", min_length=1, max_length=100, description="Content type"
    )
    project_id: str = Field(..., min_length=1, description="Project ID")
    library_id: str = Field(..., min_length=1, description="Asset library ID")


class ChunkedUploadInitResponse(BaseModel):
    upload_id: str = Field(..., description="Upload ID")
    chunk_size: int = Field(..., description="Chunk size in bytes")
    total_chunks: int = Field(..., description="Total number of chunks")
    filename: str = Field(..., description="Filename")
    expires_at: datetime = Field(..., description="Expiration time")


class ChunkedUploadStatusResponse(BaseModel):
    upload_id: str = Field(..., description="Upload ID")
    filename: str = Field(..., description="Filename")
    file_size: int = Field(..., description="File size")
    total_chunks: int = Field(..., description="Total number of chunks")
    uploaded_chunks: list[int] = Field(..., description="List of uploaded chunk indices")
    status: str = Field(..., description="Upload status: pending/uploading/completed/failed")
    created_at: datetime = Field(..., description="Creation time")
    expires_at: datetime = Field(..., description="Expiration time")


class ChunkedUploadCompleteRequest(BaseModel):
    project_id: str = Field(..., min_length=1, description="Project ID")
    library_id: str = Field(..., min_length=1, description="Asset library ID")
    file_hash: str = Field(default="", max_length=64, description="文件 MD5 哈希，用于去重检测")


class ChunkedUploadCompleteResponse(BaseModel):
    storage_key: str = Field(..., description="Storage key")
    ingest_job_id: str = Field(..., description="Ingest job ID")
    url: str = Field(..., description="File URL")
    duplicated: bool = Field(default=False, description="是否为重复素材（命中去重）")
    asset_id: str = Field(default="", description="重复素材的 asset_id（duplicated=true 时返回）")
