package com.red.circle.other.app.convertor;

import com.red.circle.other.app.dto.clientobject.PropCouponCO;
import com.red.circle.other.app.dto.clientobject.PropCouponRecordCO;
import com.red.circle.other.domain.propcoupon.PropCoupon;
import com.red.circle.other.domain.propcoupon.PropCouponSource;
import com.red.circle.other.domain.propcoupon.PropCouponStatus;
import com.red.circle.other.domain.propcoupon.PropCouponType;
import com.red.circle.other.infra.database.rds.entity.props.PropCouponDO;
import com.red.circle.other.infra.database.rds.entity.props.PropCouponUseRecordDO;
import com.red.circle.other.infra.utils.ZonedDateTimeUtils;

import java.sql.Timestamp;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 道具券对象转换器
 *
 * @author system
 * @date 2025-11-05
 */
public class PropCouponConvertor {

    /**
     * Domain转CO
     */
    public static PropCouponCO toClientObject(PropCoupon propCoupon) {
        if (propCoupon == null) {
            return null;
        }

        PropCouponCO co = new PropCouponCO();
        co.setId(propCoupon.getId());
        co.setCouponNo(propCoupon.getCouponNo());
        co.setCouponType(propCoupon.getCouponType().getCode());
        co.setCouponTypeName(propCoupon.getCouponType().getDesc());
        co.setPropId(propCoupon.getPropId());
        co.setPropName(propCoupon.getPropName());
        co.setPropIcon(propCoupon.getPropIcon());
        co.setValidDays(propCoupon.getValidDays());
        co.setPropDays(propCoupon.getPropDays());
        co.setStatus(propCoupon.getStatus().getCode());
        co.setStatusName(propCoupon.getStatus().getDesc());
        co.setSource(propCoupon.getSource().getCode());
        co.setSourceName(propCoupon.getSource().getDesc());

        Timestamp timestamp = ZonedDateTimeUtils.toTimestampDefault(propCoupon.getExpireTime());
        co.setExpireTime(timestamp);
        co.setExpireSeconds(Math.abs(Duration.between(propCoupon.getExpireTime(), LocalDateTime.now()).getSeconds()));
        co.setUseTime(ZonedDateTimeUtils.toTimestampDefault(propCoupon.getUseTime()));
        co.setCreateTime(ZonedDateTimeUtils.toTimestampDefault(propCoupon.getCreateTime()));

        return co;
    }

    /**
     * DO转CO
     */
    public static PropCouponCO toClientObject(PropCouponDO propCouponDO) {
        if (propCouponDO == null) {
            return null;
        }

        PropCouponCO co = new PropCouponCO();
        co.setId(propCouponDO.getId());
        co.setCouponNo(propCouponDO.getCouponNo());
        co.setCouponType(propCouponDO.getCouponType());
        
        PropCouponType couponType = PropCouponType.getByCode(propCouponDO.getCouponType());
        if (couponType != null) {
            co.setCouponTypeName(couponType.getDesc());
        }
        
        co.setPropId(propCouponDO.getPropId());
        co.setPropName(propCouponDO.getPropName());
        co.setPropIcon(propCouponDO.getPropIcon());
        co.setValidDays(propCouponDO.getValidDays());
        co.setPropDays(propCouponDO.getPropDays());
        co.setStatus(propCouponDO.getStatus());
        
        PropCouponStatus status = PropCouponStatus.getByCode(propCouponDO.getStatus());
        if (status != null) {
            co.setStatusName(status.getDesc());
        }
        
        co.setSource(propCouponDO.getSource());
        
        PropCouponSource source = PropCouponSource.getByCode(propCouponDO.getSource());
        if (source != null) {
            co.setSourceName(source.getDesc());
        }

        co.setExpireTime(ZonedDateTimeUtils.toTimestampDefault(propCouponDO.getExpireTime()));
        co.setUseTime(ZonedDateTimeUtils.toTimestampDefault(propCouponDO.getUseTime()));
        co.setCreateTime(ZonedDateTimeUtils.toTimestampDefault(propCouponDO.getCreateTime()));

        return co;
    }

    /**
     * DO转RecordCO
     */
    public static PropCouponRecordCO toRecordClientObject(PropCouponUseRecordDO recordDO) {
        if (recordDO == null) {
            return null;
        }

        PropCouponRecordCO co = new PropCouponRecordCO();
        co.setId(recordDO.getId());
        co.setCouponNo(recordDO.getCouponNo());
        co.setCouponType(recordDO.getCouponType());
        
        PropCouponType couponType = PropCouponType.getByCode(recordDO.getCouponType());
        if (couponType != null) {
            co.setCouponTypeName(couponType.getDesc());
        }
        
        co.setPropId(recordDO.getPropId());
        co.setPropIcon(recordDO.getPropIcon());
        co.setPropName(recordDO.getPropName());
        co.setValidDays(recordDO.getValidDays());
        co.setPropDays(recordDO.getPropDays());
        co.setActionType(recordDO.getActionType());
        
        // 操作类型名称
        if (recordDO.getActionType() != null) {
            co.setActionTypeName(recordDO.getActionType() == 1 ? "使用" : "赠送");
        }
        
        co.setReceiverId(recordDO.getReceiverId());
        co.setRemark(recordDO.getRemark());
        co.setCreateTime(ZonedDateTimeUtils.toTimestampDefault(recordDO.getCreateTime()));

        return co;
    }

    /**
     * Domain转DO
     */
    public static PropCouponDO toDO(PropCoupon propCoupon) {
        if (propCoupon == null) {
            return null;
        }

        PropCouponDO propCouponDO = new PropCouponDO();
        propCouponDO.setId(propCoupon.getId());
        propCouponDO.setCouponNo(propCoupon.getCouponNo());
        propCouponDO.setUserId(propCoupon.getUserId());
        propCouponDO.setCouponType(propCoupon.getCouponType().getCode());
        propCouponDO.setPropId(propCoupon.getPropId());
        propCouponDO.setPropName(propCoupon.getPropName());
        propCouponDO.setValidDays(propCoupon.getValidDays());
        propCouponDO.setStatus(propCoupon.getStatus().getCode());
        propCouponDO.setSource(propCoupon.getSource().getCode());
        propCouponDO.setExpireTime(propCoupon.getExpireTime());
        propCouponDO.setUseTime(propCoupon.getUseTime());
        propCouponDO.setCreateTime(propCoupon.getCreateTime());
        propCouponDO.setUpdateTime(propCoupon.getUpdateTime());

        return propCouponDO;
    }

    /**
     * DO转Domain
     */
    public static PropCoupon toDomain(PropCouponDO propCouponDO) {
        if (propCouponDO == null) {
            return null;
        }

        PropCoupon propCoupon = new PropCoupon();
        propCoupon.setId(propCouponDO.getId());
        propCoupon.setCouponNo(propCouponDO.getCouponNo());
        propCoupon.setUserId(propCouponDO.getUserId());
        propCoupon.setCouponType(PropCouponType.getByCode(propCouponDO.getCouponType()));
        propCoupon.setPropId(propCouponDO.getPropId());
        propCoupon.setPropName(propCouponDO.getPropName());
        propCoupon.setValidDays(propCouponDO.getValidDays());
        propCoupon.setStatus(PropCouponStatus.getByCode(propCouponDO.getStatus()));
        propCoupon.setSource(PropCouponSource.getByCode(propCouponDO.getSource()));
        propCoupon.setExpireTime(propCouponDO.getExpireTime());
        propCoupon.setUseTime(propCouponDO.getUseTime());
        propCoupon.setCreateTime(propCouponDO.getCreateTime());
        propCoupon.setUpdateTime(propCouponDO.getUpdateTime());

        return propCoupon;
    }
}
