package com.seer.teach.mp.admin.service.impl;

import com.seer.teach.common.constants.CommonConstant;
import com.seer.teach.common.utils.CommonUtils;
import com.seer.teach.mp.admin.controller.req.ActivityFormReq;
import com.seer.teach.mp.admin.controller.resp.AdminActivityFormResp;
import com.seer.teach.mp.admin.convert.AdminActivityFormConvert;
import com.seer.teach.mp.admin.service.IAdminActivityFormService;
import com.seer.teach.mp.entity.MpActivityFormEntity;
import com.seer.teach.mp.entity.MpActivityFormRelationEntity;
import com.seer.teach.mp.service.IMpActivityFormRelationService;
import com.seer.teach.mp.service.IMpActivityFormService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * <p>
 * 管理端活动表单模板服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2026-01-10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminActivityFormServiceImpl implements IAdminActivityFormService {

    private final IMpActivityFormService activityFormService;

    private final IMpActivityFormRelationService activityFormRelationService;

    @Override
    public Boolean save(ActivityFormReq request) {
        MpActivityFormEntity form = AdminActivityFormConvert.INSTANCE.convertToEntity(request);
        boolean saveResult = activityFormService.save(form);

        if (saveResult) {
            MpActivityFormRelationEntity relation = new MpActivityFormRelationEntity();
            relation.setActivityId(request.getActivityId());
            relation.setFormId(form.getId());
            Optional<MpActivityFormRelationEntity> optionalRelation = activityFormRelationService.getLatestVersionByActivityId(request.getActivityId());
            if (optionalRelation.isPresent()) {
                relation.setIsPrimary(CommonConstant.IS_NOT_DEFAULT);
                relation.setVersion(CommonUtils.generateVersionNumber(optionalRelation.get().getVersion()));
            } else {
                relation.setIsPrimary(CommonConstant.IS_DEFAULT);
                relation.setVersion("1.0.0");
            }
            boolean saved = activityFormRelationService.save(relation);
            log.info("保存关联关系:{}", saved);
        }
        return saveResult;
    }

    @Override
    public Boolean deleteById(Integer id) {
        return activityFormService.removeById(id);
    }

    @Override
    public AdminActivityFormResp getById(Integer id) {
        if (id == null) {
            return null;
        }
        MpActivityFormEntity entity = activityFormService.getById(id);
        return AdminActivityFormConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public Boolean update(ActivityFormReq request) {
        if (request.getId() == null) {
            return false;
        }
        MpActivityFormEntity form = AdminActivityFormConvert.INSTANCE.convertToEntity(request);
        return activityFormService.updateById(form);
    }

    @Override
    public Boolean publishNewVersion(ActivityFormReq req) {
        Integer activityId = req.getActivityId();
        if (activityId == null) {
            return false;
        }
        Optional<MpActivityFormRelationEntity> optionalRelation = activityFormRelationService.getLatestVersionByActivityId(activityId);
        if (optionalRelation.isEmpty()) {
            return false;
        }
        MpActivityFormRelationEntity latestRelation = optionalRelation.get();

        // 复制当前表单信息创建新版本
        MpActivityFormEntity newForm = AdminActivityFormConvert.INSTANCE.convertToEntity(req);
        // 保存新的表单记录
        boolean saved = activityFormService.save(newForm);
        if (!saved) {
            log.error("publishNewVersion失败:{}", newForm);
            return false;
        }
        MpActivityFormRelationEntity newRelation = new MpActivityFormRelationEntity();
        newRelation.setActivityId(req.getActivityId());
        newRelation.setFormId(req.getId());
        newRelation.setIsPrimary(CommonConstant.IS_NOT_DEFAULT);
        newRelation.setVersion(CommonUtils.generateVersionNumber(latestRelation.getVersion()));
        return activityFormRelationService.save(newRelation);
    }
}