package p79;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import net.jqwik.api.*;
import net.jqwik.api.lifecycle.AfterContainer;
import p79.GroundTruth;

public class TestCaseGenerator {

    static class TestCase {
        Map<String, Object> Inputs;
        Object Expected;
        TestCase(Map<String, Object> inputs, Object expected) {
            this.Inputs = inputs;
            this.Expected = expected;
        }
    }

    static List<TestCase> generatedCases = new ArrayList<>();
    static AtomicInteger caseCount = new AtomicInteger(0);
    static final int MAX_CASES = 500;

    @Property(tries = 10000)
    void generateTestCases(@ForAll("float32") float x, @ForAll("float32") float y, @ForAll("float32") float lx, @ForAll("float32") float ly) {
        if (caseCount.get() >= MAX_CASES) {
            return;
        }

        float[] result;
        try {
            result = GroundTruth.getClampedAnalogPosition(x, y, lx, ly);
        } catch (Exception e) {
            return;
        }

        if (Float.isNaN(result[0]) || Float.isNaN(result[1]) || Float.isInfinite(result[0]) || Float.isInfinite(result[1])) {
            return;
        }

        Map<String, Object> inputs = new LinkedHashMap<>();
        inputs.put("x", x);
        inputs.put("y", y);
        inputs.put("lx", lx);
        inputs.put("ly", ly);
        generatedCases.add(new TestCase(inputs, result));
        caseCount.incrementAndGet();
    }

    @Example
    void example1() {
        float[] result = GroundTruth.getClampedAnalogPosition(0.0F, 0.0F, 0.0F, 0.0F);
        Map<String, Object> inputs = new LinkedHashMap<>();
        inputs.put("x", 0.0F);
        inputs.put("y", 0.0F);
        inputs.put("lx", 0.0F);
        inputs.put("ly", 0.0F);
        generatedCases.add(new TestCase(inputs, result));
        caseCount.incrementAndGet();
    }

    @Example
    void example2() {
        float[] result = GroundTruth.getClampedAnalogPosition(10.0F, 10.0F, 1.0F, 1.0F);
        Map<String, Object> inputs = new LinkedHashMap<>();
        inputs.put("x", 10.0F);
        inputs.put("y", 10.0F);
        inputs.put("lx", 1.0F);
        inputs.put("ly", 1.0F);
        generatedCases.add(new TestCase(inputs, result));
        caseCount.incrementAndGet();
    }

    @Example
    void example3() {
        float[] result = GroundTruth.getClampedAnalogPosition(10.0F, 10.0F, 2.0F, 2.0F);
        Map<String, Object> inputs = new LinkedHashMap<>();
        inputs.put("x", 10.0F);
        inputs.put("y", 10.0F);
        inputs.put("lx", 2.0F);
        inputs.put("ly", 2.0F);
        generatedCases.add(new TestCase(inputs, result));
        caseCount.incrementAndGet();
    }

    @Provide
    Arbitrary<Float> float32() {
        return Arbitraries.floats().between(-Float.MAX_VALUE, Float.MAX_VALUE);
    }

    @AfterContainer
    static void saveTestCases() throws IOException {
        if (generatedCases.isEmpty()) {
            return;
        }
        String dirPath = "src/test/java/test_cases";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String filePath = dirPath + "/test_cases.json";
        Gson gson = new GsonBuilder().setPrettyPrinting().serializeSpecialFloatingPointValues().create();
        try (Writer writer = new FileWriter(filePath)) {
            gson.toJson(generatedCases, writer);
        }
        System.out.println("✅ Saved " + generatedCases.size() + " test cases to " + filePath);
    }
}