package com.sikey.wa04.business.infrastructure.adapter.dao;

import com.sikey.wa04.business.domain.adapter.GroupAdapter;
import com.sikey.wa04.business.domain.entity.ChildId;
import com.sikey.wa04.business.domain.entity.Group;
import com.sikey.wa04.business.domain.entity.GroupId;
import com.sikey.wa04.business.domain.entity.UserId;
import com.sikey.wa04.business.infrastructure.dto.model.GroupMemberModel;
import com.sikey.wa04.business.infrastructure.dto.model.GroupModel;
import com.sikey.wa04.business.infrastructure.repository.GroupMemberRepository;
import com.sikey.wa04.business.infrastructure.repository.GroupRepository;
import com.sikey.wa04.common.enums.GroupMemberRole;
import com.sikey.wa04.common.enums.UserType;
import com.sikey.wa04.common.exception.errors.Errors;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Limit;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Slf4j
@Component
public class GroupAdapterImpl implements GroupAdapter {

    @Resource
    private GroupRepository groupRepository;

    @Resource
    private GroupMemberRepository groupMemberRepository;

    @Override
    public void initialize(GroupId groupId, ChildId childId, UserId userId) {
        GroupModel groupModel = new GroupModel();
        groupModel.setId(groupId.getId());
        groupModel.setChildId(childId.getId());
        groupRepository.save(groupModel);

        List<GroupMemberModel> memberModels = List.of(
                new GroupMemberModel()
                        .setGroupId(groupId.getId())
                        .setRefId(userId.getId())
                        .setRole(GroupMemberRole.OWNER.getValue())
                        .setType(UserType.USER.getValue()),
                new GroupMemberModel()
                        .setGroupId(groupId.getId())
                        .setRefId(childId.getId())
                        .setRole(GroupMemberRole.EVERYMAN.getValue())
                        .setType(UserType.CHILD.getValue())
        );
        groupMemberRepository.saveAll(memberModels);
    }

    @Override
    public Group findById(GroupId gid) {
        Optional<GroupModel> optional = groupRepository.findById(gid.getId());
        GroupModel groupModel = optional.orElseThrow();
        return new Group()
                .setId(new GroupId(groupModel.getId()))
                .setChildId(groupModel.getChildId());
    }

    @Override
    public Group findByChildId(ChildId cid) {
        GroupModel groupModel = groupRepository.findByChildId(cid.getId(), Limit.of(1));

        // 判断组
        Optional.ofNullable(groupModel).orElseThrow(() -> Errors.FAMILY_GROUPS_DO_NOT_EXIST.getException());

        return new Group()
                .setId(new GroupId(groupModel.getId()))
                .setChildId(groupModel.getChildId());
    }

    @Override
    public void deleteByChildId(String childId) {
        groupRepository.deleteByChildId(childId);
    }
}
