package cn.novalon.gym.manage.sys.audit.dto;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

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

/**
 * AuditLogQueryRequest 单元测试
 * 
 * @author 张翔
 * @date 2026-04-14
 */
class AuditLogQueryRequestTest {

    @Test
    @DisplayName("创建默认查询请求")
    void createDefaultRequest_shouldHaveNullFields() {
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        
        assertNull(request.getEntityType());
        assertNull(request.getEntityId());
        assertNull(request.getOperator());
        assertNull(request.getOperationType());
        assertNull(request.getStartTime());
        assertNull(request.getEndTime());
    }

    @Test
    @DisplayName("设置和获取实体类型")
    void setAndGetEntityType_shouldWorkCorrectly() {
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        request.setEntityType("User");
        
        assertEquals("User", request.getEntityType());
    }

    @Test
    @DisplayName("设置和获取实体ID")
    void setAndGetEntityId_shouldWorkCorrectly() {
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        request.setEntityId(100L);
        
        assertEquals(100L, request.getEntityId());
    }

    @Test
    @DisplayName("设置和获取操作人")
    void setAndGetOperator_shouldWorkCorrectly() {
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        request.setOperator("admin");
        
        assertEquals("admin", request.getOperator());
    }

    @Test
    @DisplayName("设置和获取操作类型")
    void setAndGetOperationType_shouldWorkCorrectly() {
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        request.setOperationType("CREATE");
        
        assertEquals("CREATE", request.getOperationType());
    }

    @Test
    @DisplayName("设置和获取开始时间")
    void setAndGetStartTime_shouldWorkCorrectly() {
        LocalDateTime startTime = LocalDateTime.now().minusDays(1);
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        request.setStartTime(startTime);
        
        assertEquals(startTime, request.getStartTime());
    }

    @Test
    @DisplayName("设置和获取结束时间")
    void setAndGetEndTime_shouldWorkCorrectly() {
        LocalDateTime endTime = LocalDateTime.now();
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        request.setEndTime(endTime);
        
        assertEquals(endTime, request.getEndTime());
    }

    @Test
    @DisplayName("toString方法应包含所有字段")
    void toString_shouldContainAllFields() {
        LocalDateTime startTime = LocalDateTime.now().minusDays(1);
        LocalDateTime endTime = LocalDateTime.now();
        
        AuditLogQueryRequest request = new AuditLogQueryRequest();
        request.setEntityType("User");
        request.setEntityId(100L);
        request.setOperator("admin");
        request.setOperationType("CREATE");
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        
        String toString = request.toString();
        
        assertTrue(toString.contains("User"));
        assertTrue(toString.contains("100"));
        assertTrue(toString.contains("admin"));
        assertTrue(toString.contains("CREATE"));
    }

    @Test
    @DisplayName("equals和hashCode方法应基于字段值")
    void equalsAndHashCode_shouldBeBasedOnFieldValues() {
        LocalDateTime startTime = LocalDateTime.now().minusDays(1);
        LocalDateTime endTime = LocalDateTime.now();
        
        AuditLogQueryRequest request1 = new AuditLogQueryRequest();
        request1.setEntityType("User");
        request1.setEntityId(100L);
        request1.setOperator("admin");
        request1.setOperationType("CREATE");
        request1.setStartTime(startTime);
        request1.setEndTime(endTime);
        
        AuditLogQueryRequest request2 = new AuditLogQueryRequest();
        request2.setEntityType("User");
        request2.setEntityId(100L);
        request2.setOperator("admin");
        request2.setOperationType("CREATE");
        request2.setStartTime(startTime);
        request2.setEndTime(endTime);
        
        assertEquals(request1, request2);
        assertEquals(request1.hashCode(), request2.hashCode());
    }

    @Test
    @DisplayName("不同字段值的对象应不相等")
    void differentFieldValues_shouldNotBeEqual() {
        AuditLogQueryRequest request1 = new AuditLogQueryRequest();
        request1.setEntityType("User");
        
        AuditLogQueryRequest request2 = new AuditLogQueryRequest();
        request2.setEntityType("Role");
        
        assertNotEquals(request1, request2);
    }
}