package vassistent.service;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import vassistent.model.ProblemLevel;

import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Integration tests for {@link AnimationFileService}.
 */
class AnimationFileServiceTest {

    @TempDir
    Path tempDir;

    /**
     * Verifies level-to-animation mapping is correctly serialized into JSON output.
     *
     * @param level problem level name
     * @param expectedAnimation expected serialized animation value
     * @throws Exception if file access fails
     */
    @ParameterizedTest
    @CsvSource({
            "NONE, no",
            "WARNING, warning",
            "HIGH, high",
            "DISASTER, disaster"
    })
    void wirteAnimationState_writesMappedAnimation(String level, String expectedAnimation) throws Exception {
        Path outputFile = tempDir.resolve("animation.json");
        AnimationFileService service = new AnimationFileService(outputFile.toString());

        service.wirteAnimationState(ProblemLevel.valueOf(level));

        assertTrue(Files.exists(outputFile));
        String content = Files.readString(outputFile);
        assertTrue(content.contains("\"animation\": \"" + expectedAnimation + "\""));
    }

    /**
     * Verifies parent directories are created automatically when missing.
     *
     * @throws Exception if file access fails
     */
    @Test
    void wirteAnimationState_createsParentDirectory() throws Exception {
        Path nestedFile = tempDir.resolve("nested/path/animation.json");
        AnimationFileService service = new AnimationFileService(nestedFile.toString());

        service.wirteAnimationState(ProblemLevel.WARNING);

        assertTrue(Files.exists(nestedFile));
    }

    /**
     * Verifies subsequent writes overwrite prior animation state.
     *
     * @throws Exception if file access fails
     */
    @Test
    void wirteAnimationState_overwritesPreviousContent() throws Exception {
        Path outputFile = tempDir.resolve("animation.json");
        AnimationFileService service = new AnimationFileService(outputFile.toString());

        service.wirteAnimationState(ProblemLevel.NONE);
        service.wirteAnimationState(ProblemLevel.DISASTER);

        String content = Files.readString(outputFile);
        assertTrue(content.contains("\"animation\": \"disaster\""));
        assertTrue(!content.contains("\"animation\": \"no\""));
    }
}
