package com.writeoff.common.util;

import com.writeoff.common.exception.BusinessException;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;

public final class ImportValidationUtils {
    private static final Pattern PHONE_PATTERN = Pattern.compile("^1\\d{10}$");
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");
    private static final Pattern ID_NO_PATTERN = Pattern.compile("(^\\d{15}$)|(^\\d{17}[\\dXx]$)");
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private ImportValidationUtils() {
    }

    public static void validatePhone(String phone) {
        String value = trim(phone);
        if (value.isEmpty()) {
            throw new BusinessException(10001, "\u624b\u673a\u53f7\u4e0d\u80fd\u4e3a\u7a7a");
        }
        if (!PHONE_PATTERN.matcher(value).matches()) {
            throw new BusinessException(10001, "\u624b\u673a\u53f7\u683c\u5f0f\u4e0d\u6b63\u786e");
        }
    }

    public static void validateOptionalEmail(String email) {
        String value = trim(email);
        if (value.isEmpty()) {
            return;
        }
        if (!EMAIL_PATTERN.matcher(value).matches()) {
            throw new BusinessException(10001, "\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e");
        }
    }

    public static void validateRequiredEmail(String email) {
        String value = trim(email);
        if (value.isEmpty()) {
            throw new BusinessException(10001, "\u90ae\u7bb1\u4e0d\u80fd\u4e3a\u7a7a");
        }
        if (!EMAIL_PATTERN.matcher(value).matches()) {
            throw new BusinessException(10001, "\u90ae\u7bb1\u683c\u5f0f\u4e0d\u6b63\u786e");
        }
    }

    public static void validateIdNo(String idNo) {
        String value = trim(idNo);
        if (value.isEmpty()) {
            throw new BusinessException(10001, "\u8eab\u4efd\u8bc1\u53f7\u4e0d\u80fd\u4e3a\u7a7a");
        }
        if (!ID_NO_PATTERN.matcher(value).matches()) {
            throw new BusinessException(10001, "\u8eab\u4efd\u8bc1\u53f7\u683c\u5f0f\u4e0d\u6b63\u786e");
        }
    }

    public static LocalDateTime parseOptionalDateTime(String raw, String fieldName) {
        String value = trim(raw);
        if (value.isEmpty()) {
            return null;
        }
        try {
            return LocalDateTime.parse(normalizeDateTime(value), DATE_TIME_FORMATTER);
        } catch (Exception ex) {
            throw new BusinessException(10001, fieldName + "\u683c\u5f0f\u4e0d\u6b63\u786e\uff0c\u5e94\u4e3a yyyy-MM-dd HH:mm:ss");
        }
    }

    public static void validateDateRange(String validFrom, String validTo) {
        LocalDateTime from = parseOptionalDateTime(validFrom, "\u751f\u6548\u65f6\u95f4");
        LocalDateTime to = parseOptionalDateTime(validTo, "\u5931\u6548\u65f6\u95f4");
        if (from != null && to != null && to.isBefore(from)) {
            throw new BusinessException(10001, "\u5931\u6548\u65f6\u95f4\u4e0d\u80fd\u65e9\u4e8e\u751f\u6548\u65f6\u95f4");
        }
    }

    public static String trim(String value) {
        return value == null ? "" : value.trim();
    }

    private static String normalizeDateTime(String value) {
        String normalized = value.replace("T", " ");
        return normalized.length() == 16 ? normalized + ":00" : normalized;
    }
}
