package p.studio.controls.shell;

import org.junit.jupiter.api.Test;
import p.packer.events.PackerEventKind;
import p.studio.lsp.events.StudioExecutionLifecycleEvent;
import p.studio.lsp.events.StudioPackerOperationEvent;
import p.studio.lsp.events.StudioProjectOpenedEvent;
import p.studio.workspaces.assets.messages.events.StudioAssetsWorkspaceRefreshFailedEvent;
import p.studio.workspaces.assets.messages.events.StudioAssetsWorkspaceRefreshedEvent;

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

final class StudioActivityEventMapperTest {
    @Test
    void mapsProjectOpenedToSuccessEntry() {
        final StudioActivityEntry entry = StudioActivityEventMapper
                .map(new StudioProjectOpenedEvent())
                .orElseThrow();

        assertEquals("Studio", entry.source());
        assertEquals(StudioActivityEntrySeverity.SUCCESS, entry.severity());
        assertEquals("Project opened", entry.message());
    }

    @Test
    void mapsAssetsRefreshedToSuccessEntry() {
        final StudioActivityEntry entry = StudioActivityEventMapper
                .map(new StudioAssetsWorkspaceRefreshedEvent(7))
                .orElseThrow();

        assertEquals("Assets", entry.source());
        assertEquals(StudioActivityEntrySeverity.SUCCESS, entry.severity());
        assertEquals("7 assets loaded", entry.message());
    }

    @Test
    void mapsRefreshFailureToStickyErrorEntry() {
        final StudioActivityEntry entry = StudioActivityEventMapper
                .map(new StudioAssetsWorkspaceRefreshFailedEvent("Refresh failed"))
                .orElseThrow();

        assertEquals(StudioActivityEntrySeverity.ERROR, entry.severity());
        assertTrue(entry.sticky());
        assertEquals("Refresh failed", entry.message());
    }

    @Test
    void mapsPackerWriteEventToActivityEntry() {
        final StudioActivityEntry entry = StudioActivityEventMapper
                .map(new StudioPackerOperationEvent("op-1", PackerEventKind.ACTION_APPLIED, "Asset created.", null, false))
                .orElseThrow();

        assertEquals("Assets", entry.source());
        assertEquals(StudioActivityEntrySeverity.SUCCESS, entry.severity());
        assertEquals("Asset created.", entry.message());
    }

    @Test
    void mapsExecutionLifecycleToActivityEntry() {
        final StudioActivityEntry entry = StudioActivityEventMapper
                .map(new StudioExecutionLifecycleEvent("Debug", "Runtime started", StudioActivityEntrySeverity.SUCCESS, false))
                .orElseThrow();

        assertEquals("Debug", entry.source());
        assertEquals(StudioActivityEntrySeverity.SUCCESS, entry.severity());
        assertEquals("Runtime started", entry.message());
    }
}
