package com.yaoai.api.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yaoai.api.dto.asset.GlobalAssetDTO;
import com.yaoai.common.exception.BizException;
import com.yaoai.common.exception.ErrorCode;
import com.yaoai.common.response.ApiResponse;
import com.yaoai.domain.entity.GlobalAsset;
import com.yaoai.domain.mapper.GlobalAssetMapper;
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 java.util.List;
import java.util.Map;

@Tag(name = "资产库")
@RestController
@RequestMapping("/assets")
@RequiredArgsConstructor
public class GlobalAssetsController {

    private final GlobalAssetMapper globalAssetMapper;

    @Operation(summary = "获取资产列表")
    @GetMapping
    public ApiResponse<List<GlobalAssetDTO>> list(
            @RequestParam(required = false) String type,
            @RequestParam(required = false, defaultValue = "personal") String library) {
        Long tenantId = TenantContext.get();
        List<GlobalAssetDTO> result = globalAssetMapper
                .findByTenantAndType(tenantId, type, library)
                .stream().map(GlobalAssetDTO::from).toList();
        return ApiResponse.success(result);
    }

    @Operation(summary = "创建资产")
    @PostMapping
    public ApiResponse<GlobalAssetDTO> create(@RequestBody Map<String, Object> body) {
        Long tenantId = TenantContext.get();
        Long creatorUserId = StpUtil.getLoginIdAsLong();

        GlobalAsset asset = new GlobalAsset();
        asset.setTenantId(tenantId);
        asset.setCreatorUserId(creatorUserId);
        asset.setName((String) body.getOrDefault("name", "未命名"));
        asset.setAssetType((String) body.getOrDefault("assetType", "character"));
        asset.setLibraryType((String) body.getOrDefault("libraryType", "personal"));
        asset.setTags((String) body.get("tags"));
        asset.setImageUrl((String) body.get("imageUrl"));
        asset.setImageTosKey((String) body.get("imageTosKey"));
        asset.setDescription((String) body.get("description"));
        asset.setIsFavorite(0);
        Object srcProject = body.get("sourceProjectId");
        if (srcProject instanceof Number) asset.setSourceProjectId(((Number) srcProject).longValue());

        globalAssetMapper.insert(asset);
        return ApiResponse.success(GlobalAssetDTO.from(asset));
    }

    @Operation(summary = "切换收藏状态")
    @PutMapping("/{id}/favorite")
    public ApiResponse<GlobalAssetDTO> toggleFavorite(@PathVariable Long id) {
        Long tenantId = TenantContext.get();
        GlobalAsset asset = globalAssetMapper.selectById(id);
        if (asset == null || !tenantId.equals(asset.getTenantId())) {
            throw new BizException(ErrorCode.NOT_FOUND, "资产不存在");
        }
        asset.setIsFavorite(Integer.valueOf(1).equals(asset.getIsFavorite()) ? 0 : 1);
        globalAssetMapper.updateById(asset);
        return ApiResponse.success(GlobalAssetDTO.from(asset));
    }

    @Operation(summary = "更新资产")
    @PutMapping("/{id}")
    public ApiResponse<GlobalAssetDTO> update(@PathVariable Long id,
                                               @RequestBody Map<String, Object> body) {
        Long tenantId = TenantContext.get();
        GlobalAsset asset = globalAssetMapper.selectById(id);
        if (asset == null || !tenantId.equals(asset.getTenantId())) {
            throw new BizException(ErrorCode.NOT_FOUND, "资产不存在");
        }
        if (body.containsKey("name")) asset.setName((String) body.get("name"));
        if (body.containsKey("tags")) asset.setTags((String) body.get("tags"));
        if (body.containsKey("description")) asset.setDescription((String) body.get("description"));
        if (body.containsKey("libraryType")) asset.setLibraryType((String) body.get("libraryType"));
        globalAssetMapper.updateById(asset);
        return ApiResponse.success(GlobalAssetDTO.from(asset));
    }

    @Operation(summary = "删除资产")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        Long tenantId = TenantContext.get();
        GlobalAsset asset = globalAssetMapper.selectById(id);
        if (asset == null || !tenantId.equals(asset.getTenantId())) {
            throw new BizException(ErrorCode.NOT_FOUND, "资产不存在");
        }
        globalAssetMapper.deleteById(id);
        return ApiResponse.success();
    }
}
