package dev.rheinsw.shared.entity;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

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

class BaseEntityTest {

    // Dummy entity for testing
    static class DummyEntity extends BaseEntity {
    }

    @Test
    void onCreate_shouldSetCreatedDateTime() {
        // Arrange
        DummyEntity entity = new DummyEntity();

        // Act
        entity.onCreate();

        // Assert
        assertNotNull(entity.getCreatedDateTime(), "createdDateTime should be set");
        assertNull(entity.getModifiedDateTime(), "modifiedDateTime should still be null after creation");
    }

    @Test
    void onUpdate_shouldSetModifiedDateTime() {
        // Arrange
        DummyEntity entity = new DummyEntity();

        // Act
        entity.onUpdate();

        // Assert
        assertNotNull(entity.getModifiedDateTime(), "modifiedDateTime should be set");
        assertNull(entity.getCreatedDateTime(), "createdDateTime should still be null if onCreate() is not called");
    }

    @Test
    void onCreate_thenOnUpdate_shouldSetBothTimestamps() throws InterruptedException {
        // Arrange
        DummyEntity entity = new DummyEntity();

        // Act
        entity.onCreate();
        LocalDateTime created = entity.getCreatedDateTime();
        Thread.sleep(10); // slight pause to differentiate timestamps
        entity.onUpdate();
        LocalDateTime modified = entity.getModifiedDateTime();

        // Assert
        assertNotNull(created);
        assertNotNull(modified);
        assertTrue(modified.isAfter(created), "modifiedDateTime should be after createdDateTime");
    }
}
