package com.developx.szpitale.ingest.source;

import org.junit.jupiter.api.Test;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

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

class MarkdownRegistrySourceTest {

    @Test
    void fetch_fromFileResource_returnsRecord() throws Exception {
        URI uri = getClass().getResource("/fixtures/research-test.md").toURI();
        Path tempDir = Files.createTempDirectory("markdown-test");
        Files.copy(Path.of(uri), tempDir.resolve("research-test.md"));

        MarkdownRegistrySource source = new MarkdownRegistrySource(tempDir, "test");
        var records = source.fetch();

        assertTrue(records.size() >= 1, "Should parse at least one hospital record, got: " + records.size());
        var record = records.get(0);
        assertNotNull(record.hospitalName());
        assertFalse(record.hospitalName().isEmpty());
        assertNotNull(record.persons());
        assertTrue(record.persons().size() >= 1,
                "Should have at least one person entry, got: " + record.persons().size());
        var person = record.persons().get(0);
        assertEquals("Jan Kowalski", person.fullName());
        assertFalse(person.sourceUrl().isEmpty());
    }

    @Test
    void fetch_rowWithoutUrl_flaggedOrSkipped() throws Exception {
        Path tempDir = Files.createTempDirectory("markdown-test");
        Files.writeString(tempDir.resolve("research-test.md"), """
                ## 1. Test Hospital 2

                <table>
                <tr><th>Funkcja</th><th>Imię i nazwisko</th><th>A</th><th>B</th></tr>
                <tr><td>Dyrektor</td><td>Anna Nowak</td><td>brak danych</td><td>BRAK_DANYSCH</td></tr>
                <tr><td>Z-ca</td><td>Piotr Wiśniewski</td><td>PO</td><td>https://szpital.pl/zca</td></tr>
                </table>
                """, StandardCharsets.UTF_8);

        MarkdownRegistrySource source = new MarkdownRegistrySource(tempDir, "test");
        var records = source.fetch();

        assertTrue(records.size() >= 1, "Should parse at least one hospital record");
        var record = records.get(0);
        int personCount = record.persons() == null ? 0 : record.persons().size();
        assertTrue(personCount >= 2,
                "Should have at least 2 person entries, got: " + personCount);

        // First person has BRAK_DANYCH - should be flagged
        var person = record.persons().get(0);
        assertTrue(
                person.sourceUrl().isEmpty() || person.sourceUrl().contains("BRAK") || person.sourceUrl().contains("brak"),
                "Person without URL should be flagged, got: " + person.sourceUrl()
        );

        // Second person has valid URL
        var person2 = record.persons().get(1);
        assertEquals("https://szpital.pl/zca", person2.sourceUrl());
    }

    @Test
    void fetch_noFile_returnsEmpty() {
        Path tempDir = Path.of("/tmp/nonexistent-markdown-test-" + System.currentTimeMillis());
        MarkdownRegistrySource source = new MarkdownRegistrySource(tempDir, "nonexistent");
        var records = source.fetch();
        assertTrue(records.isEmpty(), "Should return empty when no research file found");
    }
}
