package com.competition.modules.pub.controller;

import com.competition.common.result.PageResult;
import com.competition.common.result.Result;
import com.competition.common.util.SecurityUtil;
import com.competition.modules.pub.service.PublicCreationService;
import com.competition.modules.ugc.entity.UgcWork;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "AI创作")
@RestController
@RequestMapping("/public/creation")
@RequiredArgsConstructor
public class PublicCreationController {

    private final PublicCreationService publicCreationService;

    @PostMapping("/submit")
    @Operation(summary = "提交AI创作")
    public Result<UgcWork> submit(@RequestBody Map<String, String> body) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicCreationService.submit(userId,
                body.get("originalImageUrl"),
                body.get("voiceInputUrl"),
                body.get("textInput")));
    }

    @GetMapping("/{id}/status")
    @Operation(summary = "获取创作状态")
    public Result<Map<String, Object>> getStatus(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicCreationService.getStatus(id, userId));
    }

    @GetMapping("/{id}/result")
    @Operation(summary = "获取创作结果")
    public Result<Map<String, Object>> getResult(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicCreationService.getResult(id, userId));
    }

    @GetMapping("/history")
    @Operation(summary = "创作历史")
    public Result<PageResult<UgcWork>> getHistory(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        Long userId = SecurityUtil.getCurrentUserId();
        return Result.success(publicCreationService.getHistory(userId, page, pageSize));
    }
}
