package com.sino.mci.channel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sino.mci.admin.security.AuthContext;
import com.sino.mci.channel.dto.ChannelIdentityResponse;
import com.sino.mci.channel.entity.ChannelSubject;
import com.sino.mci.channel.repository.ChannelSubjectMapper;
import com.sino.mci.crm.entity.ChannelIdentity;
import com.sino.mci.crm.entity.Person;
import com.sino.mci.crm.repository.ChannelIdentityMapper;
import com.sino.mci.crm.repository.PersonMapper;
import com.sino.mci.shared.web.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1/channel-identity")
@RequiredArgsConstructor
public class ChannelIdentityController {

    private final ChannelIdentityMapper channelIdentityMapper;
    private final PersonMapper personMapper;
    private final ChannelSubjectMapper channelSubjectMapper;

    @GetMapping("/list")
    public ApiResponse<List<ChannelIdentityResponse>> listChannelIdentities() {
        String tenantCode = AuthContext.currentTenantCode();
        List<ChannelIdentity> identities = channelIdentityMapper.selectList(
                new LambdaQueryWrapper<ChannelIdentity>()
                        .eq(ChannelIdentity::getTenantCode, tenantCode)
                        .orderByDesc(ChannelIdentity::getId));

        if (identities.isEmpty()) {
            return ApiResponse.ok(Collections.emptyList());
        }

        Set<Long> personIds = identities.stream()
                .map(ChannelIdentity::getPersonId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Set<Long> subjectIds = identities.stream()
                .map(ChannelIdentity::getSubjectId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());

        Map<Long, Person> personMap = personIds.isEmpty() ? Collections.emptyMap()
                : personMapper.selectBatchIds(personIds).stream()
                        .collect(Collectors.toMap(Person::getId, p -> p));
        Map<Long, ChannelSubject> subjectMap = subjectIds.isEmpty() ? Collections.emptyMap()
                : channelSubjectMapper.selectBatchIds(subjectIds).stream()
                        .collect(Collectors.toMap(ChannelSubject::getId, s -> s));

        List<ChannelIdentityResponse> responses = identities.stream().map(identity -> {
            ChannelIdentityResponse response = new ChannelIdentityResponse();
            response.setId(identity.getId());
            response.setTenantCode(identity.getTenantCode());
            response.setChannelType(identity.getChannelType());
            response.setChannelUserId(identity.getChannelUserId());
            response.setChannelUserName(identity.getChannelUserName());
            response.setSubjectId(identity.getSubjectId());
            ChannelSubject subject = identity.getSubjectId() == null ? null : subjectMap.get(identity.getSubjectId());
            response.setSubjectName(subject == null ? null : subject.getSubjectName());
            response.setPersonId(identity.getPersonId());
            Person person = identity.getPersonId() == null ? null : personMap.get(identity.getPersonId());
            response.setPersonName(person == null ? null : person.getPersonName());
            response.setPersonType(person == null ? null : person.getPersonType());
            response.setStatus(identity.getStatus());
            return response;
        }).collect(Collectors.toList());

        return ApiResponse.ok(responses);
    }
}
