package com.yaoai.api.controller;

import com.yaoai.common.response.ApiResponse;
import com.yaoai.domain.entity.Character;
import com.yaoai.domain.entity.CharacterState;
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 = "获取项目角色状态图列表")
    @GetMapping("/states")
    public ApiResponse<List<CharacterState>> listAllStates(@PathVariable Long projectId) {
        return ApiResponse.success(assetService.listCharacterStates(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 = "获取角色状态/造型列表")
    @GetMapping("/{characterId}/states")
    public ApiResponse<List<CharacterState>> listStates(@PathVariable Long projectId,
                                                        @PathVariable Long characterId) {
        return ApiResponse.success(assetService.listCharacterStates(projectId, characterId, TenantContext.get()));
    }

    @Operation(summary = "新建或更新角色状态/造型")
    @PostMapping("/{characterId}/states")
    public ApiResponse<CharacterState> saveState(@PathVariable Long projectId,
                                                 @PathVariable Long characterId,
                                                 @RequestBody CharacterState req) {
        return ApiResponse.success(assetService.saveCharacterState(projectId, characterId, TenantContext.get(), req));
    }

    @Operation(summary = "基于角色参考图生成状态图")
    @PostMapping("/{characterId}/states/{stateId}/generate-image")
    public ApiResponse<CharacterState> generateStateImage(@PathVariable Long projectId,
                                                          @PathVariable Long characterId,
                                                          @PathVariable Long stateId) {
        return ApiResponse.success(assetService.generateCharacterStateImage(projectId, characterId, stateId, TenantContext.get()));
    }

    @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();
    }

    @Operation(summary = "删除角色状态/造型")
    @DeleteMapping("/{characterId}/states/{stateId}")
    public ApiResponse<Void> deleteState(@PathVariable Long projectId,
                                         @PathVariable Long characterId,
                                         @PathVariable Long stateId) {
        assetService.deleteCharacterState(projectId, characterId, stateId, TenantContext.get());
        return ApiResponse.success();
    }
}
