package com.agent.management.controller;

import com.agent.management.common.Result;
import com.agent.management.model.dto.TagExportDTO;
import com.agent.management.model.dto.TagImportDTO;
import com.agent.management.model.dto.TagImportResultDTO;
import com.agent.management.model.entity.Tag;
import com.agent.management.model.entity.TagGroup;
import com.agent.management.model.vo.TagBriefVO;
import com.agent.management.service.TagService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/tags")
@RequiredArgsConstructor
public class TagController {

    private final TagService tagService;

    // ========== 标签组 ==========

    @GetMapping("/groups")
    public Result<List<TagGroup>> listGroups() {
        return Result.success(tagService.listGroups());
    }

    @PostMapping("/groups")
    public Result<TagGroup> createGroup(@RequestBody CreateGroupReq req) {
        return Result.success(tagService.createGroup(req.getName(), req.getDescription()));
    }

    @PostMapping("/groups/{id}/edit")
    public Result<TagGroup> updateGroup(@PathVariable Long id, @RequestBody UpdateGroupReq req) {
        return Result.success(tagService.updateGroup(id, req.getName(), req.getDescription()));
    }

    @PostMapping("/groups/{id}/delete")
    public Result<Void> deleteGroup(@PathVariable Long id) {
        tagService.deleteGroup(id);
        return Result.success(null);
    }

    // ========== 标签节点 ==========

    @GetMapping("/groups/{groupId}/tags")
    public Result<List<Tag>> listTags(@PathVariable Long groupId) {
        return Result.success(tagService.listTags(groupId));
    }

    @PostMapping("/groups/{groupId}/tags")
    public Result<Tag> createTag(@PathVariable Long groupId, @RequestBody CreateTagReq req) {
        return Result.success(tagService.createTag(groupId, req.getParentId(), req.getName()));
    }

    @PostMapping("/{id}/edit")
    public Result<Tag> updateTag(@PathVariable Long id, @RequestBody UpdateTagReq req) {
        return Result.success(tagService.updateTag(id, req.getName()));
    }

    @PostMapping("/{id}/delete")
    public Result<Void> deleteTag(@PathVariable Long id) {
        tagService.deleteTag(id);
        return Result.success(null);
    }

    @PostMapping("/{id}/move")
    public Result<Tag> moveTag(@PathVariable Long id, @RequestBody MoveTagReq req) {
        return Result.success(tagService.moveTag(id, req.getParentId(), req.getSortOrder()));
    }

    // ========== 导入导出 ==========

    @GetMapping("/export")
    public Result<TagExportDTO> exportTags() {
        return Result.success(tagService.exportAll());
    }

    @PostMapping("/import")
    public Result<TagImportResultDTO> importTags(@RequestBody TagImportDTO req) {
        return Result.success(tagService.importTags(req));
    }

    // ========== 打标 ==========

    /** 设置实体的标签（全量替换） */
    @PostMapping("/assignments")
    public Result<Void> setTags(@RequestBody SetTagsReq req) {
        tagService.setTags(req.getEntityType(), req.getEntityId(), req.getTagIds());
        return Result.success(null);
    }

    /** 获取实体的标签列表 */
    @GetMapping("/assignments")
    public Result<List<TagBriefVO>> getEntityTags(
            @RequestParam String entityType,
            @RequestParam String entityId) {
        return Result.success(tagService.getEntityTags(entityType, entityId));
    }

    /** 获取所有标签组及其标签（用于标签选择器） */
    @GetMapping("/all-with-tags")
    public Result<List<Map<String, Object>>> listAllWithTags() {
        List<TagGroup> groups = tagService.listGroups();
        List<Map<String, Object>> result = new java.util.ArrayList<>();
        for (TagGroup g : groups) {
            Map<String, Object> item = new java.util.LinkedHashMap<>();
            item.put("id", g.getId());
            item.put("name", g.getName());
            item.put("tags", tagService.listTags(g.getId()));
            result.add(item);
        }
        return Result.success(result);
    }

    // ========== 请求体 ==========

    @Data
    public static class CreateGroupReq {
        private String name;
        private String description;
    }

    @Data
    public static class UpdateGroupReq {
        private String name;
        private String description;
    }

    @Data
    public static class CreateTagReq {
        private Long parentId;
        private String name;
    }

    @Data
    public static class UpdateTagReq {
        private String name;
    }

    @Data
    public static class MoveTagReq {
        private Long parentId;
        private Integer sortOrder;
    }

    @Data
    public static class SetTagsReq {
        private String entityType;
        private String entityId;
        private List<Long> tagIds;
    }
}
