package com.lesingle.creation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.ai3d.AI3DTaskQueryDTO;
import com.lesingle.creation.dto.ai3d.CreateAI3DTaskDTO;
import com.lesingle.creation.service.AI3DTaskService;
import com.lesingle.creation.vo.ai3d.AI3DTaskVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * AI 3D 生成控制器
 */
@Tag(name = "AI 3D 生成")
@RestController
@RequestMapping("/api/ai-3d")
@RequiredArgsConstructor
public class AI3DTaskController {

    private final AI3DTaskService ai3dTaskService;

    @PostMapping
    @Operation(summary = "创建 AI 3D 任务")
    public Result<AI3DTaskVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateAI3DTaskDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long userId = userPrincipal.getUserId();
        AI3DTaskVO result = ai3dTaskService.create(dto, tenantId, userId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取任务详情")
    public Result<AI3DTaskVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        AI3DTaskVO result = ai3dTaskService.getDetail(id, tenantId);
        return Result.success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询任务列表")
    public Result<Page<AI3DTaskVO>> pageQuery(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @ModelAttribute AI3DTaskQueryDTO queryDTO) {
        Long tenantId = userPrincipal.getTenantId();
        Long userId = userPrincipal.getUserId(); // 查询自己的任务
        Page<AI3DTaskVO> result = ai3dTaskService.pageQuery(queryDTO, tenantId, userId);
        return Result.success(result);
    }

    @PutMapping("/{id}/cancel")
    @Operation(summary = "取消任务")
    public Result<Void> cancel(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        ai3dTaskService.cancel(id, tenantId);
        return Result.success(null);
    }

    @PutMapping("/{id}/retry")
    @Operation(summary = "重试失败的任务")
    public Result<AI3DTaskVO> retry(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        AI3DTaskVO result = ai3dTaskService.retry(id, tenantId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除 AI 3D 任务")
    @PreAuthorize("hasAuthority('ai-3d:delete')")
    public Result<Void> delete(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        ai3dTaskService.delete(id, tenantId);
        return Result.success(null);
    }
}
