package shop.alien.util.common;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 账号注销统一时间线（用户端 / 商家端 / 律师端一致）：
 * <ul>
 *   <li>0~7 天：冷静期，可撤销注销</li>
 *   <li>7~14 天：已注销，不可登录/注册</li>
 *   <li>14 天后：同手机号可重新注册</li>
 * </ul>
 */
public final class AccountLogoutUtil {

    public static final int COOLING_DAYS = 7;
    public static final int RE_REGISTER_BLOCK_DAYS = 14;
    public static final String LOGGED_OUT_MESSAGE = "当前用户已注销";

    private static final DateTimeFormatter RE_REGISTER_ALLOW_TIME_FORMAT =
            DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
    private static final DateTimeFormatter COOLING_END_TIME_FORMAT =
            DateTimeFormatter.ofPattern("yyyy/MM/dd");

    private AccountLogoutUtil() {
    }

    /** 是否仍在 0~7 天冷静期内（可撤销注销） */
    public static boolean isWithinCoolingPeriod(Date logoutTime) {
        return logoutTime != null && !isLogoutCoolingExpired(logoutTime);
    }

    /** 冷静期是否已结束（满 7 天） */
    public static boolean isLogoutCoolingExpired(Date logoutTime) {
        if (logoutTime == null) {
            return false;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(logoutTime);
        calendar.add(Calendar.DAY_OF_YEAR, COOLING_DAYS);
        return new Date().compareTo(calendar.getTime()) >= 0;
    }

    /** 是否处于 7~14 天禁登/禁注册期（自申请注销日起算） */
    public static boolean isWithinReRegisterBlockPeriod(Date logoutTime) {
        if (logoutTime == null) {
            return false;
        }
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime allowFrom = logoutTime.toInstant()
                .atZone(zone)
                .toLocalDate()
                .plusDays(RE_REGISTER_BLOCK_DAYS)
                .atStartOfDay();
        return LocalDateTime.now(zone).isBefore(allowFrom);
    }

    public static String resolveCoolingEndTime(Date logoutTime) {
        if (logoutTime == null) {
            return null;
        }
        return logoutTime.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate()
                .plusDays(COOLING_DAYS)
                .format(COOLING_END_TIME_FORMAT);
    }

    public static String resolveReRegisterAllowTime(Date logoutTime) {
        if (logoutTime == null) {
            return null;
        }
        return logoutTime.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate()
                .plusDays(RE_REGISTER_BLOCK_DAYS)
                .atStartOfDay()
                .format(RE_REGISTER_ALLOW_TIME_FORMAT);
    }

    /** 登录/已注销态提示（7~14 天） */
    public static String loggedOutBlockMessage(Date logoutTime, Integer logoutFlagDone) {
        if (logoutFlagDone == null || logoutFlagDone != 1) {
            return null;
        }
        if (isWithinReRegisterBlockPeriod(logoutTime)) {
            return LOGGED_OUT_MESSAGE;
        }
        return null;
    }

    /** 注册拦截提示（7~14 天） */
    public static String reRegisterBlockedMessage(Date logoutTime, Integer logoutFlagDone) {
        if (logoutFlagDone == null || logoutFlagDone != 1) {
            return null;
        }
        if (!isWithinReRegisterBlockPeriod(logoutTime)) {
            return null;
        }
        String allowTime = resolveReRegisterAllowTime(logoutTime);
        if (allowTime == null) {
            return LOGGED_OUT_MESSAGE;
        }
        return "原手机号无法注册新账号，" + allowTime + "起可重新注册";
    }
}
