package com.ruoyi.web.modules.entry.support;

import java.util.Date;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.entry.dto.EntryApplyShopDTO;
import com.ruoyi.web.modules.entry.dto.EntryApplySubmitDTO;
import com.ruoyi.web.modules.merchant.constant.MerchantConstants;
import com.ruoyi.web.modules.merchant.domain.BizMerchant;

/**
 * C 端入驻申请表单校验（移动端完整字段）
 */
public final class EntryApplyFormValidator
{
    private EntryApplyFormValidator()
    {
    }

    public static void validateMobileSubmit(EntryApplySubmitDTO dto)
    {
        if (dto == null || dto.getSubject() == null || dto.getBiz() == null || dto.getShop() == null)
        {
            throw new ServiceException("请完整填写入驻申请信息");
        }
        String merchantType = resolveMerchantType(dto);
        if (MerchantConstants.TYPE_PERSON.equals(merchantType))
        {
            validatePersonSubject(dto.getSubject());
            validatePersonBiz(dto.getBiz());
        }
        else if (MerchantConstants.TYPE_ENTERPRISE.equals(merchantType))
        {
            validateEnterpriseSubject(dto.getSubject());
            validateEnterpriseBiz(dto.getBiz());
        }
        else
        {
            throw new ServiceException("商户类型无效");
        }
        validateShop(dto.getShop(), merchantType);
    }

    public static String resolveMerchantType(EntryApplySubmitDTO dto)
    {
        if (dto.getSubject() != null && StringUtils.isNotEmpty(dto.getSubject().getMerchantType()))
        {
            return dto.getSubject().getMerchantType();
        }
        if (dto.getBiz() != null && StringUtils.isNotEmpty(dto.getBiz().getMerchantType()))
        {
            return dto.getBiz().getMerchantType();
        }
        throw new ServiceException("商户类型无效");
    }

    /** 个人 — 个人信息（含联系人、结算银行） */
    private static void validatePersonSubject(BizMerchant s)
    {
        requireText(s.getPersonName(), "请输入姓名");
        requireText(s.getResidenceAddress(), "请输入居住地址");
        requireText(s.getGender(), "请选择性别");
        requireNotNull(s.getBirthDate(), "请选择出生日期");
        requireIdCardType(s.getIdCardType(), "请选择证件类型");
        requireText(s.getIdCardNo(), "请输入证件号码");
        requireValidPeriod("证件", s.getIdValidType(), s.getIdValidStart(), s.getIdValidEnd());
        requireText(s.getIdCardFront(), "请上传证件照正面");
        requireText(s.getIdCardBack(), "请上传证件照反面");
        requireText(s.getContactName(), "请输入联系人姓名");
        requireText(s.getContactPhone(), "请输入联系人手机");
        requireText(s.getContactEmail(), "请输入常用邮箱");
        requireText(s.getBankName(), "请输入开户银行");
        requireText(s.getBankBranch(), "请输入开户支行");
        requireText(s.getBankAccount(), "请输入银行账号");
    }

    /** 个人 — 经营信息 */
    private static void validatePersonBiz(BizMerchant b)
    {
        requireText(b.getMerchantName(), "请输入商户名称");
        requireText(b.getServicePhone(), "请输入客服电话");
        requireRegion(b.getBizRegionCode(), b.getBizRegionName(), "请选择经营地址");
        requireText(b.getBizDetailAddress(), "请输入经营详细地址");
    }

    /** 企业 — 企业信息 + 法人信息 */
    private static void validateEnterpriseSubject(BizMerchant s)
    {
        requireText(s.getCompanyName(), "请输入企业名称");
        requireRegion(s.getRegRegionCode(), s.getRegRegionName(), "请选择注册地址");
        requireText(s.getCompanyDetailAddress(), "请输入注册详细地址");
        requireText(s.getBusinessScope(), "请输入经营范围");
        requireText(s.getCreditCode(), "请输入统一社会信用代码");
        requireValidPeriod("营业", s.getLicenseValidType(), s.getLicenseValidStart(), s.getLicenseValidEnd());

        requireText(s.getLegalName(), "请输入法人姓名");
        requireText(s.getLegalResidence(), "请输入法人居住地址");
        requireText(s.getLegalGender(), "请选择法人性别");
        requireNotNull(s.getLegalBirthDate(), "请选择法人出生日期");
        requireIdCardType(s.getLegalIdCardType(), "请选择法人证件类型");
        requireText(s.getLegalIdCardNo(), "请输入法人证件号码");
        requireValidPeriod("法人证件", s.getLegalIdValidType(), s.getLegalIdValidStart(), s.getLegalIdValidEnd());
        requireText(s.getLegalIdCardFront(), "请上传法人证件照正面");
        requireText(s.getLegalIdCardBack(), "请上传法人证件照反面");
        requireText(s.getContactName(), "请输入联系人姓名");
        requireText(s.getContactPhone(), "请输入联系人手机");
        requireText(s.getContactEmail(), "请输入常用邮箱");
        requireText(s.getBankName(), "请输入开户银行");
        requireText(s.getBankBranch(), "请输入开户支行");
        requireText(s.getBankAccount(), "请输入银行账号");
        requireText(s.getCorpBankAccount(), "请输入对公银行账号");
        requireText(s.getAccountPermit(), "请上传开户许可证");
    }

    /** 企业 — 经营信息（不含商户名称/客服电话） */
    private static void validateEnterpriseBiz(BizMerchant b)
    {
        requireRegion(b.getBizRegionCode(), b.getBizRegionName(), "请选择经营地址");
        requireText(b.getBizDetailAddress(), "请输入经营详细地址");
        requireText(b.getBusinessLicense(), "请上传营业执照");
    }

    private static void validateShop(EntryApplyShopDTO shop, String merchantType)
    {
        requireText(shop.getShopName(), "请输入店铺名称");
        requireText(shop.getShopPhone(), "请输入联系电话");
        requireText(shop.getShopAvatar(), "请上传店铺Logo");
        // shopDesc 选填
    }

    private static void requireText(String value, String message)
    {
        if (StringUtils.isEmpty(value))
        {
            throw new ServiceException(message);
        }
    }

    private static void requireNotNull(Date value, String message)
    {
        if (value == null)
        {
            throw new ServiceException(message);
        }
    }

    private static void requireRegion(String code, String name, String message)
    {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(name))
        {
            throw new ServiceException(message);
        }
    }

    private static void requireIdCardType(String type, String message)
    {
        if (StringUtils.isEmpty(type))
        {
            throw new ServiceException(message);
        }
        if (!MerchantConstants.ID_TYPE_MAINLAND.equals(type)
                && !MerchantConstants.ID_TYPE_HK_PASS.equals(type))
        {
            throw new ServiceException(message);
        }
    }

    private static void requireValidPeriod(String label, String validType, Date start, Date end)
    {
        if (StringUtils.isEmpty(validType))
        {
            throw new ServiceException("请选择" + label + "有效期类型");
        }
        if (MerchantConstants.VALID_PERIOD_RANGE.equals(validType))
        {
            if (start == null || end == null)
            {
                throw new ServiceException("请填写" + label + "有效期");
            }
        }
    }
}
