package com.ruoyi.system.domain.fieldchangerecord.enums;

import com.ruoyi.system.domain.changerecord.enums.SubjectType;

/**
 * 字段级变更的业务语义字段码。
 *
 * <p>一共 10 个字段，覆盖需求方指定的全部统计维度：</p>
 * <ul>
 *     <li>HR 侧 6 个：业务场所状态 / 面积 / 产权人联系方式 / 租金单价 / 场所图片 / 营业执照图片</li>
 *     <li>BE 侧 4 个：名称 / 负责人联系方式 / 工作人员数量 / 年度销售额</li>
 * </ul>
 *
 * <p>{@link #propertyName} 是对应 Java 实体 (HouseResource / BusinessEntityInfo /
 * BusinessEntitySell) 上的属性名，拦截器据此反射取值做 diff。</p>
 *
 * <p>{@code BE_YEAR_SELL} 较特殊：subject_id 取 {@code business_entity_info_id}，
 * old / new value 序列化为 {@code year=2025;value=523.50} 形式，拦截器在 diff 时按
 * 这个格式组装。</p>
 */
public enum FieldCode {

    // --- BUSINESS_ENTITY_INFO ---

    BE_NAME(SubjectType.BUSINESS_ENTITY_INFO, FieldValueType.TEXT,
            "name", "经营主体名称"),

    BE_PRINCIPAL_TEL(SubjectType.BUSINESS_ENTITY_INFO, FieldValueType.TEXT,
            "principalTel", "负责人联系方式"),

    BE_WORKER_NUMBER(SubjectType.BUSINESS_ENTITY_INFO, FieldValueType.NUMBER,
            "workerNumber", "工作人员数量"),

    /**
     * 年度销售额。subject 挂在 BUSINESS_ENTITY_INFO 上，值以
     * {@code year=2025;value=523.50} 形式存储。
     */
    BE_YEAR_SELL(SubjectType.BUSINESS_ENTITY_INFO, FieldValueType.NUMBER,
            "yearSell", "年度销售额"),

    // --- HOUSE_RESOURCE ---

    HR_BUSINESS_STATUS(SubjectType.HOUSE_RESOURCE, FieldValueType.ENUM,
            "businessStatus", "经营场所状态"),

    HR_AREA(SubjectType.HOUSE_RESOURCE, FieldValueType.NUMBER,
            "houseArea", "经营场所面积"),

    HR_EQUITY_TEL(SubjectType.HOUSE_RESOURCE, FieldValueType.TEXT,
            "houseResourceEquityTel", "产权人联系方式"),

    HR_UNIT_PRICE(SubjectType.HOUSE_RESOURCE, FieldValueType.NUMBER,
            "unitPrice", "租金"),

    HR_IMAGE(SubjectType.HOUSE_RESOURCE, FieldValueType.IMAGE,
            "houseResourceUrl", "经营场所图片"),

    HR_LICENSE_IMAGE(SubjectType.HOUSE_RESOURCE, FieldValueType.IMAGE,
            "businessLicenseUrl", "营业执照图片");

    private final SubjectType subjectType;

    private final FieldValueType valueType;

    /** 对应 Java 实体上的属性名。拦截器据此反射取值。 */
    private final String propertyName;

    /** 中文 label，接口层返回给前端展示。 */
    private final String label;

    FieldCode(SubjectType subjectType, FieldValueType valueType, String propertyName, String label) {
        this.subjectType = subjectType;
        this.valueType = valueType;
        this.propertyName = propertyName;
        this.label = label;
    }

    public SubjectType getSubjectType() {
        return subjectType;
    }

    public FieldValueType getValueType() {
        return valueType;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public String getLabel() {
        return label;
    }

    /** 根据 code 字符串反查枚举；未知 code 返回 null（不抛异常，便于前端容错）。 */
    public static FieldCode fromCode(String code) {
        if (code == null) {
            return null;
        }
        for (FieldCode f : values()) {
            if (f.name().equals(code)) {
                return f;
            }
        }
        return null;
    }
}
