package com.ycwl.basic.controller.pc;

import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
import com.ycwl.basic.model.pc.source.req.SourceReqQuery;
import com.ycwl.basic.model.printer.req.CreateVirtualOrderRequest;
import com.ycwl.basic.service.pc.SourceService;
import com.ycwl.basic.service.printer.PrinterService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author：longbinbin
 * @Date：2024/12/3 15:45
 */
@RestController
@RequestMapping("/api/source/v1")
// 视频源管理
public class SourceController {

    @Autowired
    private SourceService sourceService;
    @Autowired
    private PrinterService printerService;

    // 分页查询视频源
    @PostMapping("/page")
    public ApiResponse pageQuery(@RequestBody SourceReqQuery sourceReqQuery) {
        return sourceService.pageQuery(sourceReqQuery);
    }
    @Deprecated
    // 查询视频源列表
    @PostMapping("/list")
    public ApiResponse list(@RequestBody SourceReqQuery sourceReqQuery) {
        return sourceService.list(sourceReqQuery);
    }
    @PostMapping("/{id}/cutVideo")
    @IgnoreToken
    public ApiResponse cutVideo(@PathVariable("id") Long id) {
        return sourceService.cutVideo(id);
    }
    @Deprecated
    // 删除视频源
    @DeleteMapping("/delete/{id}")
    public ApiResponse deleteById(@PathVariable Long id) {
        return sourceService.deleteById(id);
    }

    /**
     * 创建虚拟用户订单
     * 用于后台直接从source创建订单，不需要真实用户
     * 支持立即0元购买或创建待支付订单（由needActualPayment控制）
     *
     * @param request 请求参数
     * @return 订单信息
     */
    @PostMapping("/createVirtualOrder")
    public ApiResponse<Map<String, Object>> createVirtualOrder(@RequestBody CreateVirtualOrderRequest request) {
        if (request.getSourceIds() == null || request.getSourceIds().isEmpty()) {
            return ApiResponse.fail("sourceIds不能为空");
        }
        try {
            Map<String, Object> result = printerService.createBatchVirtualOrder(
                    request.getSourceIds(),
                    request.getScenicId(),
                    request.getPrinterId(),
                    request.getNeedEnhance(),
                    request.getPrintImgUrl(),
                    request.getNeedActualPayment()
            );
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.fail(e.getMessage());
        }
    }

    /**
     * 根据sourceId列表查询关联的faceId
     * @param sourceIds sourceId列表
     * @return sourceId -> faceId 的映射，无关联则value为null
     */
    @PostMapping("/faceIds")
    public ApiResponse<Map<Long, Long>> getFaceIdsBySourceIds(@RequestBody List<Long> sourceIds) {
        return sourceService.getFaceIdsBySourceIds(sourceIds);
    }

    /**
     * 根据faceId分页查询关联的source记录
     * @param sourceReqQuery 查询参数（需设置faceId，可选type/scenicId/isBuy）
     * @return 分页source列表
     */
    @PostMapping("/pageByFaceId")
    public ApiResponse pageByFaceId(@RequestBody SourceReqQuery sourceReqQuery) {
        return sourceService.pageByFaceId(sourceReqQuery);
    }

    /**
     * 管理员取消关联（软删除）
     * @param id member_source 记录 ID
     * @return 操作结果
     */
    @PostMapping("/admin/cancel/{id}")
    public ApiResponse cancelRelation(@PathVariable("id") Long id) {
        return sourceService.cancelRelation(id);
    }

    /**
     * 管理员恢复已取消的关联
     * @param id member_source 记录 ID
     * @return 操作结果
     */
    @PostMapping("/admin/reactivate/{id}")
    public ApiResponse reactivateRelation(@PathVariable("id") Long id) {
        return sourceService.reactivateRelation(id);
    }

    /**
     * 管理员查询已取消的关联记录
     * @param sourceReqQuery 查询参数（需设置faceId，可选type/scenicId）
     * @return 分页已取消关联列表
     */
    @PostMapping("/admin/pageDeletedByFaceId")
    public ApiResponse pageDeletedByFaceId(@RequestBody SourceReqQuery sourceReqQuery) {
        return sourceService.pageDeletedByFaceId(sourceReqQuery);
    }

}
