package org.raddatz.familienarchiv.ocr;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class OcrStreamEventTest {

    @Test
    void startRecordHoldsTotalPages() {
        var start = new OcrStreamEvent.Start(5);
        assertThat(start.totalPages()).isEqualTo(5);
        assertThat(start).isInstanceOf(OcrStreamEvent.class);
    }

    @Test
    void pageRecordHoldsBlocksAndPageNumber() {
        var block = new OcrBlockResult(0, 0.1, 0.2, 0.8, 0.1, null, "Test", null);
        var page = new OcrStreamEvent.Page(0, List.of(block));
        assertThat(page.pageNumber()).isEqualTo(0);
        assertThat(page.blocks()).hasSize(1);
    }

    @Test
    void errorRecordHoldsPageAndMessage() {
        var error = new OcrStreamEvent.Error(2, "failed");
        assertThat(error.pageNumber()).isEqualTo(2);
        assertThat(error.message()).isEqualTo("failed");
    }

    @Test
    void doneRecordHoldsTotalBlocksAndSkippedPages() {
        var done = new OcrStreamEvent.Done(12, 2);
        assertThat(done.totalBlocks()).isEqualTo(12);
        assertThat(done.skippedPages()).isEqualTo(2);
    }

    @Test
    void preprocessingRecordHoldsPageNumber() {
        var preprocessing = new OcrStreamEvent.Preprocessing(4);
        assertThat(preprocessing.pageNumber()).isEqualTo(4);
        assertThat(preprocessing).isInstanceOf(OcrStreamEvent.class);
    }

    @Test
    void patternMatchingWorksOnSealedInterface() {
        OcrStreamEvent event = new OcrStreamEvent.Start(3);
        String result = switch (event) {
            case OcrStreamEvent.Start s -> "start:" + s.totalPages();
            case OcrStreamEvent.Preprocessing pre -> "preprocessing:" + pre.pageNumber();
            case OcrStreamEvent.Page p -> "page:" + p.pageNumber();
            case OcrStreamEvent.Error e -> "error:" + e.pageNumber();
            case OcrStreamEvent.Done d -> "done:" + d.totalBlocks();
        };
        assertThat(result).isEqualTo("start:3");
    }
}
