package p.studio.projects;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

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

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

final class KnownProjectsServiceTest {
    @TempDir
    Path tempDir;

    @Test
    void seedsKnownProjectsFromCatalogWhenStorageIsEmpty() throws Exception {
        final Path projectsRoot = tempDir.resolve("projects");
        Files.createDirectories(projectsRoot.resolve("alpha"));
        Files.createDirectories(projectsRoot.resolve("beta"));
        Files.writeString(projectsRoot.resolve("alpha").resolve("prometeu.json"), manifest("Alpha Project"));
        Files.writeString(projectsRoot.resolve("beta").resolve("prometeu.json"), manifest("Beta Project"));

        final KnownProjectsService service = new KnownProjectsService(
                tempDir.resolve("known-projects.txt"),
                new ProjectCatalogService(projectsRoot));

        assertEquals(
                java.util.List.of("Alpha Project", "Beta Project"),
                service.listKnownProjects().stream().map(ProjectReference::name).toList());
    }

    @Test
    void canForgetProjectWithoutDeletingDirectory() throws Exception {
        final Path projectsRoot = tempDir.resolve("projects");
        Files.createDirectories(projectsRoot.resolve("alpha"));
        Files.writeString(projectsRoot.resolve("alpha").resolve("prometeu.json"), manifest("Alpha Project"));
        final ProjectReference alpha = new ProjectReference("Alpha Project", "1.0.0", "pbs", 1, projectsRoot.resolve("alpha"));

        final KnownProjectsService service = new KnownProjectsService(
                tempDir.resolve("known-projects.txt"),
                new ProjectCatalogService(projectsRoot));

        service.remember(alpha);
        service.forget(alpha);

        assertEquals(java.util.List.of(), service.listKnownProjects());
        assertEquals(true, Files.isDirectory(alpha.rootPath()));
    }

    @Test
    void canRememberProjectAgainAfterItWasForgotten() throws Exception {
        final Path projectsRoot = tempDir.resolve("projects");
        Files.createDirectories(projectsRoot.resolve("alpha"));
        Files.writeString(projectsRoot.resolve("alpha").resolve("prometeu.json"), manifest("Alpha Project"));
        final ProjectReference alpha = new ProjectReference("Alpha Project", "1.0.0", "pbs", 1, projectsRoot.resolve("alpha"));

        final KnownProjectsService service = new KnownProjectsService(
                tempDir.resolve("known-projects.txt"),
                new ProjectCatalogService(projectsRoot));

        service.remember(alpha);
        service.forget(alpha);
        service.remember(alpha);

        assertEquals(
                java.util.List.of("Alpha Project"),
                service.listKnownProjects().stream().map(ProjectReference::name).toList());
    }

    private String manifest(String name) {
        return """
                {
                  "name": "%s",
                  "version": "1.0.0",
                  "language": "pbs",
                  "stdlib": "1",
                  "dependencies": []
                }
                """.formatted(name);
    }
}
