package com.red.circle.other.infra.database.mongo.service.bd;

import com.red.circle.other.infra.database.mongo.entity.bd.BdSalaryPolicy;
import com.red.circle.other.infra.enums.BdPolicyType;
import com.red.circle.tool.core.date.TimestampUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * BD 政策服务实现.
 *
 * @author AI Assistant
 * @since 2025-10-29
 */
@Service
@RequiredArgsConstructor
public class BdSalaryPolicyServiceImpl implements BdSalaryPolicyService {

    private final MongoTemplate mongoTemplate;

    @Override
    public void create(BdSalaryPolicy policy) {
        policy.setCreateTime(TimestampUtils.now());
        policy.setUpdateTime(TimestampUtils.now());
        mongoTemplate.save(policy);
    }

    @Override
    public void batchCreate(List<BdSalaryPolicy> policies) {
        policies.forEach(policy -> {
            policy.setCreateTime(TimestampUtils.now());
            policy.setUpdateTime(TimestampUtils.now());
        });
        mongoTemplate.insertAll(policies);
    }

    @Override
    public void update(BdSalaryPolicy policy) {
        policy.setUpdateTime(TimestampUtils.now());
        mongoTemplate.save(policy);
    }

    @Override
    public BdSalaryPolicy getById(String id) {
        return mongoTemplate.findById(id, BdSalaryPolicy.class);
    }

    @Override
    public List<BdSalaryPolicy> listEnabled() {
        Query query = new Query(Criteria.where("enabled").is(true));
        query.with(Sort.by(Sort.Order.asc("policyType"), Sort.Order.asc("level")));
        return mongoTemplate.find(query, BdSalaryPolicy.class);
    }

    @Override
    public List<BdSalaryPolicy> listEnabledByType(BdPolicyType policyType) {
        Query query = new Query(Criteria.where("enabled").is(true)
                .and("policyType").is(policyType.name()));
        query.with(Sort.by(Sort.Order.asc("level")));
        return mongoTemplate.find(query, BdSalaryPolicy.class);
    }

    @Override
    public BdSalaryPolicy getByTypeAndLevel(BdPolicyType policyType, Integer level) {
        Query query = new Query(Criteria.where("policyType").is(policyType.name())
                .and("level").is(level)
                .and("enabled").is(true));
        return mongoTemplate.findOne(query, BdSalaryPolicy.class);
    }

    @Override
    public void enable(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update()
                .set("enabled", true)
                .set("updateTime", TimestampUtils.now());
        mongoTemplate.updateFirst(query, update, BdSalaryPolicy.class);
    }

    @Override
    public void disable(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update()
                .set("enabled", false)
                .set("updateTime", TimestampUtils.now());
        mongoTemplate.updateFirst(query, update, BdSalaryPolicy.class);
    }
}
