package p.studio.compiler.workspaces;

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

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

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

class AssetSurfaceContextLoaderTest {

    @TempDir
    Path tempDir;

    @Test
    void shouldLoadOnlyIncludedAssetsAsAddressables() throws IOException {
        final var registryDir = tempDir.resolve("assets").resolve(".prometeu");
        Files.createDirectories(registryDir);
        Files.writeString(registryDir.resolve("index.json"), """
                {
                  "schema_version": 1,
                  "next_asset_id": 4,
                  "assets": [
                    { "asset_id": 3, "root": "ui/atlas2", "included_in_build": true },
                    { "asset_id": 7, "root": "ui/one-more-atlas", "included_in_build": false },
                    { "asset_id": 8, "root": "ui/sound" }
                  ]
                }
                """);

        final var surface = new AssetSurfaceContextLoader().load(tempDir);

        assertEquals(2, surface.assets().size());
        assertEquals("assets.ui.atlas2", surface.assets().get(0).address());
        assertEquals(3, surface.assets().get(0).assetId());
        assertEquals("assets.ui.sound", surface.assets().get(1).address());
        assertEquals(8, surface.assets().get(1).assetId());
    }

    @Test
    void shouldReturnEmptySurfaceWhenRegistryIsMissing() {
        final var surface = new AssetSurfaceContextLoader().load(tempDir);

        assertTrue(surface.assets().isEmpty());
    }
}
