package com.magistr.app.model;

import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.LocalDate;

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

class LifecycleEntityTest {

    @Test
    void archivedEntityIsNotActiveOnDateWithoutActiveTo() {
        Subject subject = new Subject();
        subject.setStatus(LifecycleEntity.STATUS_ARCHIVED);

        assertThat(subject.isActiveOn(LocalDate.now())).isFalse();
    }

    @Test
    void archivedEntityRemainsActiveForHistoricalDatesBeforeActiveTo() {
        Subject subject = new Subject();
        subject.setStatus(LifecycleEntity.STATUS_ARCHIVED);
        subject.setActiveTo(LocalDate.of(2026, 5, 10));

        assertThat(subject.isActiveOn(LocalDate.of(2026, 5, 9))).isTrue();
        assertThat(subject.isActiveOn(LocalDate.of(2026, 5, 11))).isFalse();
    }

    @Test
    void activeEntityUsesDateRange() {
        Subject subject = new Subject();
        subject.setActiveFrom(LocalDate.of(2026, 1, 10));
        subject.setActiveTo(LocalDate.of(2026, 1, 20));

        assertThat(subject.isActiveOn(LocalDate.of(2026, 1, 9))).isFalse();
        assertThat(subject.isActiveOn(LocalDate.of(2026, 1, 10))).isTrue();
        assertThat(subject.isActiveOn(LocalDate.of(2026, 1, 20))).isTrue();
        assertThat(subject.isActiveOn(LocalDate.of(2026, 1, 21))).isFalse();
    }

    @Test
    void archiveUsesExplicitBusinessDateAndUtcMoment() {
        Subject subject = new Subject();
        LocalDate businessDate = LocalDate.of(2026, 1, 2);
        Instant archiveMoment = Instant.parse("2026-01-01T21:00:00Z");

        subject.archive("Тестовая архивация", businessDate, archiveMoment);

        assertThat(subject.getActiveTo()).isEqualTo(businessDate);
        assertThat(subject.getArchivedAt()).isEqualTo(archiveMoment);
        assertThat(subject.getArchiveReason()).isEqualTo("Тестовая архивация");
    }
}
