package com.developx.szpitale.load;

import com.developx.szpitale.model.*;
import com.developx.szpitale.model.enums.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

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

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

class CanonicalRoundTripTest {

    @TempDir
    Path tempDir;

    @Test
    void write_thenRead_yieldsEqualDataset() throws Exception {
        Hospital hospital = new Hospital(
            "hosp:podlaskie:usk-bialystok",
            "Uniwersytecki Szpital Kliniczny w Białymstoku",
            "USK",
            "Białystok",
            "podlaskie",
            LegalForm.SPZOZ,
            "Uniwersytet Medyczny",
            SupervisoryBodyType.RADA_SPOLECZNA,
            null, null,
            "https://uskwb.pl",
            List.of("https://uskwb.pl/dyrekcja")
        );

        Person person = new Person(
            "person:jan-kowalski",
            "Jan Kowalski",
            "Dyrektor Naczelny",
            List.of("prof."),
            List.of("https://uskwb.pl/dyrekcja")
        );

        Affiliation affiliation = new Affiliation(
            "person:jan-kowalski",
            AffiliationType.PARTIA,
            "Koalicja Obywatelska",
            ConfidenceLevel.POTWIERDZONA,
            "PO",
            List.of("https://uskwb.pl/dyrekcja")
        );

        Role role = new Role(
            "hosp:podlaskie:usk-bialystok",
            "person:jan-kowalski",
            RoleType.DYREKTOR,
            "Dyrektor Naczelny",
            OrganType.DYREKCJA,
            RoleStatus.AKTUALNY,
            null, null,
            null,
            List.of("https://uskwb.pl/dyrekcja")
        );

        CanonicalDataset original = new CanonicalDataset(
            "1.0", "podlaskie",
            java.time.LocalDateTime.now(),
            List.of(hospital),
            List.of(person),
            List.of(affiliation),
            List.of(role),
            List.of()
        );

        Path canonicalDir = tempDir.resolve("canonical");
        Files.createDirectories(canonicalDir);

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        mapper.writerWithDefaultPrettyPrinter().writeValue(
            canonicalDir.resolve("podlaskie.json").toFile(), original
        );

        CanonicalReader reader = new CanonicalReader();
        CanonicalDataset readBack = reader.readSingle(tempDir, "podlaskie");

        assertNotNull(readBack);
        assertEquals(original.schemaVersion(), readBack.schemaVersion());
        assertEquals(original.voivodeship(), readBack.voivodeship());
        assertEquals(1, readBack.hospitals().size());
        assertEquals(hospital.id(), readBack.hospitals().get(0).id());
        assertEquals(hospital.name(), readBack.hospitals().get(0).name());
        assertEquals(1, readBack.people().size());
        assertEquals(person.id(), readBack.people().get(0).id());
        assertEquals(person.fullName(), readBack.people().get(0).fullName());
        assertEquals(1, readBack.affiliations().size());
        assertEquals(affiliation.name(), readBack.affiliations().get(0).name());
        assertEquals(1, readBack.roles().size());
        assertEquals(role.hospitalId(), readBack.roles().get(0).hospitalId());
        assertEquals(role.roleType(), readBack.roles().get(0).roleType());
    }
}
