package ru.nbch.credit_tracker.utils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.util.stream.Stream;

@SpringBootTest
public class CTUtilTest {

    private static Stream<Arguments> dateParsingTestCases() {
        return Stream.of(
                Arguments.of(
                        "ABС123_MY_BATCH_01.1.online.2025-04-30T18-05-42.xml.gz",
                        LocalDateTime.of(2025, 4, 30, 18, 5, 42)
                ),
                Arguments.of(
                        "ABС1234_MY_BATCH_01.2.online.2025-05-05T11-25-41.xml.gz",
                        LocalDateTime.of(2025, 5, 5, 11, 25, 41)
                )
                ,
                Arguments.of(
                        "asfwqfwqwgqg",
                        null
                )
                ,
                Arguments.of(
                        "ABС1234_MY_BATCH_01.2.online.2023-04-04T12-23-14.xml.gz",
                        LocalDateTime.of(2023, 4, 4, 12, 23, 14)
                )
        );
    }

    private static Stream<Arguments> busCategoryTestCases() {
        return Stream.of(
                Arguments.of(
                        "MFO",
                        true
                ),
                Arguments.of(
                        "MKK",
                        true
                )
                ,
                Arguments.of(
                        "MFK",
                        true
                )
                ,
                Arguments.of(
                        "BANK",
                        false
                )
                ,
                Arguments.of(
                        "",
                        false
                )
                ,
                Arguments.of(
                        null,
                        false
                )
        );
    }

    private static Stream<Arguments> memberCodeParsingTestCases() {
        return Stream.of(
                Arguments.of(
                        "0001XX_MY_BATCH_71_20250703_180647.zip",
                        "0001XX"
                ),
                Arguments.of(
                        "000222_BATCH_72_20250703_180647.zip",
                        "000222"
                )
                ,
                Arguments.of(
                        "000333_BATCH_73_20250703_180647.zip",
                        "000333"
                )
                ,
                Arguments.of(
                        "0001XXBATCH732025070380647.zip",
                        null
                )
                ,
                Arguments.of(
                        "",
                        null
                )
                ,
                Arguments.of(
                        null,
                        null
                )
        );
    }

    private static Stream<Arguments> normalizingTestCases() {
        return Stream.of(
                Arguments.of(
                        "ADE124115",
                        "ADE124115"
                ),
                Arguments.of(
                        "A__DE12___4115",
                        "ADE124115"
                )
                ,
                Arguments.of(
                        "A__DE12  %$#@$%12___4115   5",
                        "ADE121241155"
                )
                ,
                Arguments.of(
                        "0001XXBATCH732025070380647.zip",
                        "0001XXBATCH732025070380647zip"
                )
                ,
                Arguments.of(
                        null,
                        null
                )
                ,
                Arguments.of(
                        "",
                        null
                )
        );
    }

    @ParameterizedTest(name = "[{index}] FILENAME: [ {0} ] EXPECTED: [ {1} ]")
    @MethodSource("dateParsingTestCases")
    void parseDateFromReportFileNameTest(String fileName, LocalDateTime expected) {
        Assertions.assertEquals(expected, CTUtil.parseDateFromReportFileName(fileName));
    }

    @ParameterizedTest(name = "[{index}] CATEGORY: [ {0} ] EXPECTED: [ {1} ]")
    @MethodSource("busCategoryTestCases")
    void checkBusCategoryTest(String category, boolean expected) {
        Assertions.assertEquals(expected, CTUtil.checkBusCategory(category));
    }

    @ParameterizedTest(name = "[{index}] FILENAME: [ {0} ] EXPECTED: [ {1} ]")
    @MethodSource("memberCodeParsingTestCases")
    void getMemberCodeFromFileNameTest(String fileName, String expected) {
        Assertions.assertEquals(expected, CTUtil.getMemberCodeFromFileName(fileName));
    }

    @ParameterizedTest(name = "[{index}] STR: [ {0} ] EXPECTED: [ {1} ]")
    @MethodSource("normalizingTestCases")
    void normalizeTest(String str, String expected) {
        Assertions.assertEquals(expected, CTUtil.normalize(str));
    }
}
