package com.ycwl.basic.controller.pc;

import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.mapper.FaceSampleAssociationMapper;
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleAssociationEntity;
import com.ycwl.basic.model.pc.faceSample.req.FaceSampleAssociationReq;
import com.ycwl.basic.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 人脸样本关联分组管理
 */
@Slf4j
@RestController
@RequestMapping("/api/faceSampleAssociation/v1")
@IgnoreToken
public class FaceSampleAssociationController {

    @Autowired
    private FaceSampleAssociationMapper faceSampleAssociationMapper;

    /**
     * 添加关联：将一批 faceSampleId 加入指定的 groupKey
     */
    @PostMapping("/add")
    public ApiResponse<String> add(@RequestBody FaceSampleAssociationReq req) {
        if (req.getScenicId() == null || req.getGroupKey() == null || req.getGroupKey().isBlank()) {
            return ApiResponse.fail("scenicId 和 groupKey 不能为空");
        }
        if (req.getFaceSampleIds() == null || req.getFaceSampleIds().isEmpty()) {
            return ApiResponse.fail("faceSampleIds 不能为空");
        }

        List<FaceSampleAssociationEntity> entities = req.getFaceSampleIds().stream()
                .map(sampleId -> {
                    FaceSampleAssociationEntity entity = new FaceSampleAssociationEntity();
                    entity.setScenicId(req.getScenicId());
                    entity.setGroupKey(req.getGroupKey());
                    entity.setFaceSampleId(sampleId);
                    return entity;
                })
                .collect(Collectors.toList());

        faceSampleAssociationMapper.batchInsertIgnore(entities);
        log.info("添加人脸样本关联: scenicId={}, groupKey={}, count={}",
                req.getScenicId(), req.getGroupKey(), entities.size());
        return ApiResponse.success("添加成功");
    }

    /**
     * 移除关联：从指定 groupKey 中移除一批 faceSampleId
     */
    @PostMapping("/remove")
    public ApiResponse<String> remove(@RequestBody FaceSampleAssociationReq req) {
        if (req.getScenicId() == null || req.getGroupKey() == null || req.getGroupKey().isBlank()) {
            return ApiResponse.fail("scenicId 和 groupKey 不能为空");
        }
        if (req.getFaceSampleIds() == null || req.getFaceSampleIds().isEmpty()) {
            return ApiResponse.fail("faceSampleIds 不能为空");
        }

        faceSampleAssociationMapper.deleteByGroupAndSampleIds(
                req.getScenicId(), req.getGroupKey(), req.getFaceSampleIds());
        log.info("移除人脸样本关联: scenicId={}, groupKey={}, count={}",
                req.getScenicId(), req.getGroupKey(), req.getFaceSampleIds().size());
        return ApiResponse.success("移除成功");
    }

    /**
     * 删除整个组
     */
    @PostMapping("/deleteGroup")
    public ApiResponse<String> deleteGroup(@RequestParam Long scenicId, @RequestParam String groupKey) {
        if (scenicId == null || groupKey == null || groupKey.isBlank()) {
            return ApiResponse.fail("scenicId 和 groupKey 不能为空");
        }

        faceSampleAssociationMapper.deleteByGroup(scenicId, groupKey);
        log.info("删除关联组: scenicId={}, groupKey={}", scenicId, groupKey);
        return ApiResponse.success("删除成功");
    }

    /**
     * 查询指定组的所有样本ID
     */
    @GetMapping("/listByGroup")
    public ApiResponse<List<Long>> listByGroup(@RequestParam Long scenicId, @RequestParam String groupKey) {
        List<Long> sampleIds = faceSampleAssociationMapper.listSampleIdsByGroup(scenicId, groupKey);
        return ApiResponse.success(sampleIds);
    }

    /**
     * 查询指定景区下的所有分组及其样本ID
     */
    @GetMapping("/listGroups")
    public ApiResponse<Map<String, List<Long>>> listGroups(@RequestParam Long scenicId) {
        List<String> groupKeys = faceSampleAssociationMapper.listGroupKeysByScenicId(scenicId);
        Map<String, List<Long>> result = groupKeys.stream()
                .collect(Collectors.toMap(
                        groupKey -> groupKey,
                        groupKey -> faceSampleAssociationMapper.listSampleIdsByGroup(scenicId, groupKey)
                ));
        return ApiResponse.success(result);
    }
}
