package com.ruoyi.system.omgpay.service;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertFalse;

class OmgPaymentAttemptI18nContractTest {

    private static final List<String> OMG_KEYS = List.of(
            "omg.payment.ddid.required",
            "omg.payment.merchantTradeNo.required",
            "omg.payment.merchantTradeNo.invalid",
            "omg.payment.storeId.required",
            "omg.payment.merchantId.required",
            "omg.payment.merchantId.invalid",
            "omg.payment.amount.invalid"
    );

    @Test
    void omgValidationKeysResolveInAllBundles() throws IOException {
        for (Path bundle : allBundles()) {
            Properties properties = loadProperties(bundle);
            for (String key : OMG_KEYS) {
                String value = properties.getProperty(key);
                assertNotNull(value,
                        () -> bundle.getFileName() + " should resolve " + key);
                assertFalse(value.isBlank(),
                        () -> bundle.getFileName() + " should provide a non-blank value for " + key);
            }
        }
    }

    private static Properties loadProperties(Path bundle) throws IOException {
        Properties properties = new Properties();
        try (Reader reader = Files.newBufferedReader(bundle, StandardCharsets.UTF_8)) {
            properties.load(reader);
        }
        return properties;
    }

    private static List<Path> allBundles() {
        return List.of(
                bundle("messages.properties"),
                bundle("messages_en_US.properties"),
                bundle("messages_vi.properties"),
                bundle("messages_zh_CN.properties"),
                bundle("messages_zh_TW.properties")
        );
    }

    private static Path bundle(String name) {
        return Paths.get("..", "ruoyi-admin", "src", "main", "resources", "i18n", name);
    }
}
