package com.aegis.resource.requisition.domain;

import java.math.BigDecimal;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import com.aegis.common.core.domain.BaseEntity;
import com.aegis.common.exception.ServiceException;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * 补偿记录实体类 resource_requisition_compensation
 * 
 * @author aegis
 */
@Getter
@Setter
@ToString(callSuper = true)
public class RequisitionCompensation extends BaseEntity {
    
    private static final long serialVersionUID = 1L;

    /** 补偿ID */
    private Long id;

    /** 征用ID */
    private Long requisitionId;

    /** 工作量（小时/天/任务数） */
    private BigDecimal workload;

    /** 工作量单位：hour/day/task */
    private String workloadUnit;

    /** 单价 */
    private BigDecimal unitPrice;

    /** 金额 */
    private BigDecimal amount;

    /** 币种 */
    private String currency;

    /** 发票号 */
    private String invoiceNo;

    /** 支付时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date paidAt;

    /** 状态：pending-待结算，settled-已结算 */
    private String status;

    /**
     * 初始化默认值
     * 
     * @param operator 操作人
     */
    public void initializeDefaults(String operator) {
        if (StringUtils.isBlank(this.status)) {
            this.status = CompensationStatus.PENDING.getCode();
        }
        if (StringUtils.isBlank(this.currency)) {
            this.currency = "CNY";
        }
        // 自动计算金额
        this.calculateAmount();
    }

    /**
     * 计算金额（工作量 × 单价）
     */
    public void calculateAmount() {
        if (this.workload != null && this.unitPrice != null) {
            this.amount = this.workload.multiply(this.unitPrice);
        }
    }

    /**
     * 结算补偿（pending → settled）
     * 
     * @param invoiceNo 发票号
     * @param paidAt 支付时间
     * @param operator 操作人
     */
    public void settle(String invoiceNo, Date paidAt, String operator) {
        CompensationStatus current = this.getStatusEnum();
        if (!CompensationStatus.PENDING.equals(current)) {
            throw new ServiceException("只有待结算状态的补偿记录才能结算");
        }
        if (StringUtils.isBlank(invoiceNo)) {
            throw new ServiceException("发票号不能为空");
        }
        if (paidAt == null) {
            throw new ServiceException("支付时间不能为空");
        }
        this.setStatusEnum(CompensationStatus.SETTLED);
        this.invoiceNo = invoiceNo;
        this.paidAt = paidAt;
        this.touch(operator);
    }

    /**
     * 获取状态枚举
     * 
     * @return 状态枚举
     */
    public CompensationStatus getStatusEnum() {
        return CompensationStatus.fromCode(status);
    }

    /**
     * 设置状态枚举
     * 
     * @param status 状态枚举
     */
    private void setStatusEnum(CompensationStatus status) {
        this.status = status.getCode();
    }

    /**
     * 获取工作量单位枚举
     * 
     * @return 工作量单位枚举
     */
    public WorkloadUnit getWorkloadUnitEnum() {
        return WorkloadUnit.fromCode(workloadUnit);
    }

    /**
     * 更新操作信息
     * 
     * @param operator 操作人
     */
    private void touch(String operator) {
        this.setUpdateBy(operator);
        this.setUpdateTime(new Date());
    }

    /**
     * 补偿状态枚举
     */
    public enum CompensationStatus {
        PENDING("pending", "待结算"),
        SETTLED("settled", "已结算");

        private final String code;
        private final String label;

        CompensationStatus(String code, String label) {
            this.code = code;
            this.label = label;
        }

        public String getCode() {
            return code;
        }

        public String getLabel() {
            return label;
        }

        public static CompensationStatus fromCode(String code) {
            if (StringUtils.isBlank(code)) {
                return PENDING;
            }
            for (CompensationStatus value : values()) {
                if (value.code.equals(code)) {
                    return value;
                }
            }
            return PENDING;
        }
    }

    /**
     * 工作量单位枚举
     */
    public enum WorkloadUnit {
        HOUR("hour", "小时"),
        DAY("day", "天"),
        TASK("task", "任务");

        private final String code;
        private final String label;

        WorkloadUnit(String code, String label) {
            this.code = code;
            this.label = label;
        }

        public String getCode() {
            return code;
        }

        public String getLabel() {
            return label;
        }

        public static WorkloadUnit fromCode(String code) {
            if (StringUtils.isBlank(code)) {
                return null;
            }
            for (WorkloadUnit value : values()) {
                if (value.code.equals(code)) {
                    return value;
                }
            }
            return null;
        }
    }
}