/*
 *  Copyright (c) 2025 John Ahlroos / Devsoap
 *
 *  Creative Commons Attribution-NoDerivatives 4.0 International Public License
 *
 *  By exercising the Licensed Rights (defined below), You accept and agree
 *  to be bound by the terms and conditions of this Creative Commons
 *  Attribution-NoDerivatives 4.0 International Public License ("Public
 *  License"). To the extent this Public License may be interpreted as a
 *  contract, You are granted the Licensed Rights in consideration of Your
 *  acceptance of these terms and conditions, and the Licensor grants You
 *  such rights in consideration of benefits the Licensor receives from
 *  making the Licensed Material available under these terms and
 *  conditions.
 *
 *  Refer to LICENSE file or https://creativecommons.org/licenses/by-nd/4.0 for full license.
 *
 */

package com.devsoap.tincheck.countries;

import com.devsoap.tincheck.tin.TinCountry;
import io.micronaut.core.util.StringUtils;
import jakarta.inject.Singleton;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.time.LocalDate;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Getter
@Singleton
@RequiredArgsConstructor
@ToString(of = {"countryCode", "countryName"})
public class Country_DE implements TinCountry {

    private final String countryCode = "DE";
    private final String countryName = "Germany";

    @Override
    public String cleanTin(String tin) {
        return TinCountry.super.cleanTin(tin).replace("/", "");
    }

    @Override
    public boolean formatCheck(String tin) {
        if (tin.length() != 11) {
            return false;
        }
        return StringUtils.isDigits(tin);
    }

    @Override
    public boolean logicCheck(String tin) {
        var digits = extractDigits(tin);
        if (digits[0] == 0) {
            return false;
        }

        boolean duplicateFound = false;
        for (int i=0; i<digits.length-2; i++) {
            for (int j=i+1; j<digits.length-1; j++) {
                duplicateFound = duplicateFound || digits[i] == digits[j];
            }
        }
        if (!duplicateFound) {
            return false;
        }
        if (digits[10] != calculateChecksum(digits,0) ||
                digits[10] != calculateChecksum(digits, 10)) {
            return false;
        }
        return true;
    }

    @Override
    public String generate(LocalDate yearOfBirth, Gender gender, Random randomGenerator) {
        var digits = randomGenerator.ints(0,10).limit(11).toArray();
        digits[0] = randomGenerator.nextInt(1,10);
        digits[randomGenerator.nextInt(1,9)] = digits[0];
        digits[10] = calculateChecksum(digits,0);
        return IntStream.of(digits).mapToObj(String::valueOf).collect(Collectors.joining());
    }

    private static int calculateChecksum(int[] digits, int z) {
        for (int i=0; i<10; i++) {
            z = calculateDigit(z, digits[i]);
        }
        var check = 11 - z;
        return check == 10 ? 0 : check;
    }

    private static int calculateDigit(int prevResult, int current) {
        var sum = (current + prevResult) % 10;
        if (sum == 0) {
            sum = 10;
        }
        return (sum * 2) % 11;
    }
}