package cn.teammodel.service.impl;

import cn.teammodel.common.ChatAppScopeEnum;
import cn.teammodel.common.ErrorCode;
import cn.teammodel.common.IdRequest;
import cn.teammodel.common.PK;
import cn.teammodel.config.exception.ServiceException;
import cn.teammodel.model.dto.ai.CreateChatAppDto;
import cn.teammodel.model.dto.ai.SearchAppDto;
import cn.teammodel.model.dto.ai.UpdateChatAppDto;
import cn.teammodel.model.entity.User;
import cn.teammodel.model.entity.ai.ChatApp;
import cn.teammodel.repository.ChatAppRepository;
import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.ChatAppService;
import cn.teammodel.utils.RepositoryUtil;
import cn.teammodel.utils.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * @author winter
 * @create 2024-01-23 11:19
 */
@Service
public class ChatAppServiceImpl implements ChatAppService {
    @Resource
    private ChatAppRepository chatAppRepostitory;

    @Override
    public ChatApp createApp(CreateChatAppDto createChatAppDto) {
        User user = SecurityUtil.getLoginUser();
        String scope = createChatAppDto.getScope();
        if (!ChatAppScopeEnum.codes().contains(scope)) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "scope参数不合法");
        }
        ChatApp newApp = new ChatApp();
        BeanUtils.copyProperties(createChatAppDto, newApp);
        newApp.setCreator(user.getName());
        newApp.setSchoolId(user.getSchoolId());
        newApp.setCreatorId(user.getId());
        newApp.setCreateTime(Instant.now().toEpochMilli());
        newApp.setCode(PK.CHAT_APP);

        return chatAppRepostitory.save(newApp);
    }

    @Override
    public ChatApp updateApp(UpdateChatAppDto updateChatAppDto) {
        String id = updateChatAppDto.getId();
        String userId = SecurityUtil.getUserId();
        ChatApp chatApp = RepositoryUtil.findOne(chatAppRepostitory.findByAppId(id), "该应用不存在");
        if (!userId.equals(chatApp.getCreatorId())) {
            throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您没有权限修改该应用");
        }
        BeanUtils.copyProperties(updateChatAppDto, chatApp);
        return chatAppRepostitory.save(chatApp);
    }

    @Override
    public void deleteApp(IdRequest idRequest) {
        String appId = idRequest.getId();
        String userId = SecurityUtil.getUserId();
        ChatApp chatApp = RepositoryUtil.findOne(chatAppRepostitory.findByAppId(appId), "该应用不存在");
        if (userId.equals(chatApp.getCreatorId())) {
            chatAppRepostitory.deleteById(appId);
        } else {
            throw new ServiceException(ErrorCode.NO_AUTH_ERROR.getCode(), "您没有权限修改该应用");
        }
    }

    @Override
    public List<ChatApp> listApp(SearchAppDto searchAppDto) {
        String scope = StrUtil.blankToNull(searchAppDto.getScope());
        String bizType = StrUtil.blankToNull(searchAppDto.getBizType());
        String lang = StrUtil.blankToNull(searchAppDto.getLang());
        String itemType = StrUtil.blankToNull(searchAppDto.getItemType());
        String period = StrUtil.blankToNull(searchAppDto.getPeriod());
        String subject = StrUtil.blankToNull(searchAppDto.getSubject());

        if (!ChatAppScopeEnum.codes().contains(scope)) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "scope参数不合法");
        }
        User user = SecurityUtil.getLoginUser();
        String schoolId = null;
        String creatorId = null;

        if (scope.equals(ChatAppScopeEnum.PRIVATE.getCode())) {
            creatorId = user.getId();
        } else if (scope.equals(ChatAppScopeEnum.SCHOOL.getCode())) {
            schoolId = user.getSchoolId();
        }

        List<ChatApp> apps = chatAppRepostitory.findAllByConditon(scope, schoolId, creatorId, bizType, lang, itemType, period, subject);
        return apps;
    }
}
