package com.purui.prims.headless.server.service.impl;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.common.pojo.enums.StatusEnum;
import com.purui.prims.common.util.BeanMapper;
import com.purui.prims.headless.api.pojo.request.QueryRuleFilter;
import com.purui.prims.headless.api.pojo.request.QueryRuleReq;
import com.purui.prims.headless.api.pojo.response.QueryRuleResp;
import com.purui.prims.headless.server.persistence.dataobject.QueryRuleDO;
import com.purui.prims.headless.server.persistence.repository.QueryRuleRepository;
import com.purui.prims.headless.server.service.DataSetService;
import com.purui.prims.headless.server.service.QueryRuleService;
import com.purui.prims.headless.server.utils.QueryRuleConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class QueryRuleServiceImpl implements QueryRuleService {

    private final QueryRuleRepository queryRuleRepository;
    private final DataSetService dataSetService;

    public QueryRuleServiceImpl(QueryRuleRepository queryRuleRepository, DataSetService dataSetService) {
        this.queryRuleRepository = queryRuleRepository;
        this.dataSetService = dataSetService;
    }

    @Override
    public QueryRuleResp addQueryRule(QueryRuleReq queryRuleReq, User user) {
        checkPermission(queryRuleReq, user);
        QueryRuleDO queryRuleDO = QueryRuleConverter.convert2DO(queryRuleReq);

        Date date = new Date();
        queryRuleDO.setCreatedBy(user.getUserName());
        queryRuleDO.setCreatedAt(date);
        queryRuleDO.setUpdatedBy(user.getUserName());
        queryRuleDO.setUpdatedAt(date);
        queryRuleDO.setStatus(StatusEnum.ONLINE.getCode());
        queryRuleDO.setId(null);

        queryRuleRepository.create(queryRuleDO);
        return getQueryRuleById(queryRuleDO.getId(), user);
    }

    @Override
    public QueryRuleResp updateQueryRule(QueryRuleReq queryRuleReq, User user) {
        checkPermission(queryRuleReq, user);
        QueryRuleDO queryRuleDO = queryRuleRepository.getQueryRuleById(queryRuleReq.getId());
        QueryRuleDO queryRuleNew = QueryRuleConverter.convert2DO(queryRuleReq);
        BeanMapper.mapper(queryRuleNew, queryRuleDO);
        queryRuleDO.setUpdatedAt(new Date());
        queryRuleDO.setUpdatedBy(user.getUserName());
        queryRuleRepository.update(queryRuleDO);
        return getQueryRuleById(queryRuleDO.getId(), user);
    }

    @Override
    public Boolean dropQueryRule(Long id, User user) {
        QueryRuleDO queryRuleDO = queryRuleRepository.getQueryRuleById(id);
        checkPermission(queryRuleDO, user);
        queryRuleDO.setStatus(StatusEnum.DELETED.getCode());
        queryRuleRepository.update(queryRuleDO);
        return true;
    }

    @Override
    public QueryRuleResp getQueryRuleById(Long id, User user) {
        QueryRuleDO queryRuleDO = queryRuleRepository.getQueryRuleById(id);
        QueryRuleResp queryRuleResp = QueryRuleConverter.convert2Resp(queryRuleDO);
        return queryRuleResp;
    }

    @Override
    public List<QueryRuleResp> getQueryRuleList(QueryRuleFilter queryRuleFilter, User user) {
        List<QueryRuleDO> queryRules = queryRuleRepository.getQueryRules(queryRuleFilter);
        List<QueryRuleResp> queryRuleRespList = QueryRuleConverter.convert2RespList(queryRules);
        return queryRuleRespList;
    }

    private void checkPermission(QueryRuleReq queryRuleReq, User user) {
        QueryRuleDO queryRuleDO = new QueryRuleDO();
        BeanMapper.mapper(queryRuleReq, queryRuleDO);
        checkPermission(queryRuleDO, user);
    }

    private void checkPermission(QueryRuleDO queryRuleDO, User user) {
        //TODO: check permission
    }

}
