from pathlib import Path
from typing import Any

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from app import schemas
from app.chain.transfer import TransferChain
from app.core.security import verify_token
from app.db import get_db
from app.schemas import MediaType

router = APIRouter()


@router.post("/manual", summary="手动转移", response_model=schemas.Response)
def manual_transfer(path: str,
                    target: str = None,
                    tmdbid: int = None,
                    type_name: str = None,
                    season: int = None,
                    transfer_type: str = None,
                    episode_format: str = None,
                    episode_detail: str = None,
                    episode_part: str = None,
                    episode_offset: int = 0,
                    min_filesize: int = 0,
                    db: Session = Depends(get_db),
                    _: schemas.TokenPayload = Depends(verify_token)) -> Any:
    """
    手动转移，支持自定义剧集识别格式
    :param path: 转移路径或文件
    :param target: 目标路径
    :param type_name: 媒体类型、电影/电视剧
    :param tmdbid: tmdbid
    :param season: 剧集季号
    :param transfer_type: 转移类型，move/copy
    :param episode_format: 剧集识别格式
    :param episode_detail: 剧集识别详细信息
    :param episode_part: 剧集识别分集信息
    :param episode_offset: 剧集识别偏移量
    :param min_filesize: 最小文件大小(MB)
    :param db: 数据库
    :param _: Token校验
    """
    in_path = Path(path)
    if target:
        target = Path(target)
        if not target.exists():
            return schemas.Response(success=False, message=f"目标路径不存在")
    # 类型
    mtype = MediaType(type_name) if type_name else None
    # 自定义格式
    epformat = None
    if episode_offset or episode_part or episode_detail or episode_format:
        epformat = schemas.EpisodeFormat(
            format=episode_format,
            detail=episode_detail,
            part=episode_part,
            offset=episode_offset,
        )
    # 开始转移
    state, errormsg = TransferChain(db).manual_transfer(
        in_path=in_path,
        target=target,
        tmdbid=tmdbid,
        mtype=mtype,
        season=season,
        transfer_type=transfer_type,
        epformat=epformat,
        min_filesize=min_filesize
    )
    # 失败
    if not state:
        if isinstance(errormsg, list):
            errormsg = f"整理完成，{len(errormsg)} 个文件转移失败！"
        return schemas.Response(success=False, message=errormsg)
    # 成功
    return schemas.Response(success=True)
