package com.yaoai.api.controller;

import com.yaoai.common.response.ApiResponse;
import com.yaoai.domain.entity.Character;
import com.yaoai.pipeline.service.AssetGenPipelineService;
import com.yaoai.security.context.TenantContext;
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 org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/projects/{projectId}/characters")
@RequiredArgsConstructor
public class CharacterController {

    private final AssetGenPipelineService assetService;

    @Operation(summary = "AI 提取角色列表")
    @PostMapping("/extract")
    public ApiResponse<List<Character>> extract(@PathVariable Long projectId) {
        return ApiResponse.success(assetService.extractCharacters(projectId, TenantContext.get()));
    }

    @Operation(summary = "获取角色列表")
    @GetMapping
    public ApiResponse<List<Character>> list(@PathVariable Long projectId) {
        return ApiResponse.success(assetService.listCharacters(projectId, TenantContext.get()));
    }

    @Operation(summary = "新建或更新角色")
    @PostMapping
    public ApiResponse<Character> save(@PathVariable Long projectId, @RequestBody Character req) {
        req.setProjectId(projectId);
        req.setTenantId(TenantContext.get());
        return ApiResponse.success(assetService.saveCharacter(req));
    }

    @Operation(summary = "AI 生成角色三视图")
    @PostMapping("/{characterId}/generate-image")
    public ApiResponse<Character> generateImage(@PathVariable Long projectId,
                                                @PathVariable Long characterId) {
        return ApiResponse.success(assetService.generateCharacterImage(characterId, TenantContext.get()));
    }

    @Operation(summary = "上传角色视图图片")
    @PostMapping(value = "/{characterId}/upload-image", consumes = "multipart/form-data")
    public ApiResponse<Character> uploadImage(@PathVariable Long projectId,
                                              @PathVariable Long characterId,
                                              @RequestParam(defaultValue = "front") String viewType,
                                              @RequestPart("file") MultipartFile file) throws IOException {
        String originalName = file.getOriginalFilename() != null ? file.getOriginalFilename() : "image.jpg";
        String ext = originalName.contains(".") ? originalName.substring(originalName.lastIndexOf('.') + 1) : "jpg";
        return ApiResponse.success(assetService.uploadCharacterImage(
                characterId,
                TenantContext.get(),
                viewType,
                file.getInputStream(),
                file.getSize(),
                file.getContentType(),
                ext));
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{characterId}")
    public ApiResponse<Void> delete(@PathVariable Long projectId, @PathVariable Long characterId) {
        assetService.deleteCharacter(characterId, TenantContext.get());
        return ApiResponse.success();
    }
}
