package org.raddatz.familienarchiv.person;

import org.junit.jupiter.api.Test;
import org.raddatz.familienarchiv.PostgresContainerConfig;
import org.raddatz.familienarchiv.person.Person;
import org.raddatz.familienarchiv.person.PersonType;
import org.raddatz.familienarchiv.person.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import software.amazon.awssdk.services.s3.S3Client;

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ActiveProfiles("test")
@Import(PostgresContainerConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class PersonServiceIntegrationTest {

    @MockitoBean S3Client s3Client;
    @Autowired PersonService personService;
    @Autowired PersonRepository personRepository;

    @Test
    void findOrCreateByAlias_skipReturnsNull_noRecordCreated() {
        Person result = personService.findOrCreateByAlias("Briefumschlag aus Java");

        assertThat(result).isNull();
        assertThat(personRepository.findAll()).isEmpty();
    }

    @Test
    void findOrCreateByAlias_institutionStoresFullNameInLastName() {
        Person result = personService.findOrCreateByAlias("Arthur Collignon GmbH");

        assertThat(result).isNotNull();
        assertThat(result.getPersonType()).isEqualTo(PersonType.INSTITUTION);
        assertThat(result.getFirstName()).isNull();
        assertThat(result.getLastName()).isEqualTo("Arthur Collignon GmbH");
        assertThat(result.getDisplayName()).isEqualTo("Arthur Collignon GmbH");
    }

    @Test
    void findOrCreateByAlias_groupStoresFullNameInLastName() {
        Person result = personService.findOrCreateByAlias("Geschwister de Gruyter");

        assertThat(result).isNotNull();
        assertThat(result.getPersonType()).isEqualTo(PersonType.GROUP);
        assertThat(result.getFirstName()).isNull();
        assertThat(result.getLastName()).isEqualTo("Geschwister de Gruyter");
    }

    @Test
    void findOrCreateByAlias_personSplitsNameNormally() {
        Person result = personService.findOrCreateByAlias("Clara Cram");

        assertThat(result).isNotNull();
        assertThat(result.getPersonType()).isEqualTo(PersonType.PERSON);
        assertThat(result.getFirstName()).isEqualTo("Clara");
        assertThat(result.getLastName()).isEqualTo("Cram");
    }
}
