package com.ycwl.basic.controller.extern;

import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.model.custom.req.AliyunCallbackReq;
import com.ycwl.basic.model.custom.req.CreateUploadTaskReq;
import com.ycwl.basic.model.custom.req.UploadCompleteReq;
import com.ycwl.basic.model.custom.req.UploadFailedReq;
import com.ycwl.basic.model.custom.resp.CreateUploadTaskResp;
import com.ycwl.basic.service.custom.CustomUploadTaskService;
import com.ycwl.basic.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/extern/custom-device")
@IgnoreToken
public class CustomDeviceController {

    @Autowired
    private CustomUploadTaskService customUploadTaskService;

    @PostMapping("/upload/create")
    public ApiResponse<CreateUploadTaskResp> createUploadTask(@RequestBody CreateUploadTaskReq req) {
        try {
            CreateUploadTaskResp resp = customUploadTaskService.createUploadTask(req);
            return ApiResponse.success(resp);
        } catch (Exception e) {
            log.error("创建上传任务失败", e);
            return ApiResponse.fail(e.getMessage());
        }
    }

    @PostMapping("/upload/complete")
    public ApiResponse<String> uploadComplete(@RequestBody UploadCompleteReq req) {
        try {
            customUploadTaskService.completeUpload(req.getAccessKey(), req.getTaskId());
            return ApiResponse.success("上传完成，人脸识别任务已提交");
        } catch (Exception e) {
            log.error("上传完成处理失败", e);
            return ApiResponse.fail(e.getMessage());
        }
    }

    @PostMapping("/upload/failed")
    public ApiResponse<String> uploadFailed(@RequestBody UploadFailedReq req) {
        try {
            customUploadTaskService.markTaskFailed(req.getAccessKey(), req.getTaskId(), req.getErrorMsg());
            return ApiResponse.success("任务已标记为失败");
        } catch (Exception e) {
            log.error("标记任务失败处理异常", e);
            return ApiResponse.fail(e.getMessage());
        }
    }

    @PostMapping("/aliyun/mps/callback")
    public ApiResponse<String> aliyunCallback(@RequestBody AliyunCallbackReq req) {
        try {
            customUploadTaskService.handleAliyunCallback(req.getJobId(), req.getStatus());
            return ApiResponse.success("回调处理完成");
        } catch (Exception e) {
            log.error("阿里云回调处理失败", e);
            return ApiResponse.fail(e.getMessage());
        }
    }

    @GetMapping("/aliyun/mps/callback")
    public ApiResponse<String> aliyunCallback(@RequestParam("jobId") String jobId, @RequestParam("status") String status) {
        try {
            customUploadTaskService.handleAliyunCallback(jobId, status);
            return ApiResponse.success("回调处理完成");
        } catch (Exception e) {
            log.error("阿里云回调处理失败", e);
            return ApiResponse.fail(e.getMessage());
        }
    }
}
