package domain

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

// TestAnnouncement_IsActiveAt 测试公告激活状态
func TestAnnouncement_IsActiveAt(t *testing.T) {
	now := time.Now()

	tests := []struct {
		name     string
		announce *Announcement
		expected bool
	}{
		{"nil", nil, false},
		{"not active status", &Announcement{Status: AnnouncementStatusDraft}, false},
		{"active no time", &Announcement{Status: AnnouncementStatusActive}, true},
		{"before start", &Announcement{Status: AnnouncementStatusActive, StartsAt: timePtr(now.Add(time.Hour))}, false},
		{"after end", &Announcement{Status: AnnouncementStatusActive, EndsAt: timePtr(now.Add(-time.Hour))}, false},
		{"active in range", &Announcement{Status: AnnouncementStatusActive, StartsAt: timePtr(now.Add(-time.Hour)), EndsAt: timePtr(now.Add(time.Hour))}, true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := tt.announce.IsActiveAt(now)
			assert.Equal(t, tt.expected, got)
		})
	}
}

// TestCustomField_TableName 测试自定义字段表名
func TestCustomField_TableName(t *testing.T) {
	cf := CustomField{}
	assert.Equal(t, "custom_fields", cf.TableName())
}

// TestUserCustomFieldValue_TableName 测试用户自定义字段值表名
func TestUserCustomFieldValue_TableName(t *testing.T) {
	cfv := UserCustomFieldValue{}
	assert.Equal(t, "user_custom_field_values", cfv.TableName())
}

// TestDevice_TableName 测试设备表名
func TestDevice_TableName(t *testing.T) {
	d := Device{}
	assert.Equal(t, "devices", d.TableName())
}

// TestLoginLog_TableName 测试登录日志表名
func TestLoginLog_TableName(t *testing.T) {
	ll := LoginLog{}
	assert.Equal(t, "login_logs", ll.TableName())
}

// TestOperationLog_TableName 测试操作日志表名
func TestOperationLog_TableName(t *testing.T) {
	ol := OperationLog{}
	assert.Equal(t, "operation_logs", ol.TableName())
}

// TestPasswordHistory_TableName 测试密码历史表名
func TestPasswordHistory_TableName(t *testing.T) {
	ph := PasswordHistory{}
	assert.Equal(t, "password_histories", ph.TableName())
}

// TestPermission_TableName 测试权限表名
func TestPermission_TableName(t *testing.T) {
	p := Permission{}
	assert.Equal(t, "permissions", p.TableName())
}

// TestRole_TableName 测试角色表名
func TestRole_TableName(t *testing.T) {
	r := Role{}
	assert.Equal(t, "roles", r.TableName())
}

// TestRolePermission_TableName 测试角色权限表名
func TestRolePermission_TableName(t *testing.T) {
	rp := RolePermission{}
	assert.Equal(t, "role_permissions", rp.TableName())
}

// TestSocialAccount_TableName 测试社交账号表名
func TestSocialAccount_TableName(t *testing.T) {
	sa := SocialAccount{}
	assert.Equal(t, "user_social_accounts", sa.TableName())
}

// TestThemeConfig_TableName 测试主题配置表名
func TestThemeConfig_TableName(t *testing.T) {
	th := ThemeConfig{}
	assert.Equal(t, "theme_configs", th.TableName())
}

// TestDefaultThemeConfig 测试默认主题配置
func TestDefaultThemeConfig(t *testing.T) {
	config := DefaultThemeConfig()
	assert.NotNil(t, config)
	assert.Equal(t, "default", config.Name)
	assert.True(t, config.IsDefault)
	assert.Equal(t, "#1890ff", config.PrimaryColor)
	assert.Equal(t, "#52c41a", config.SecondaryColor)
	assert.Equal(t, "#ffffff", config.BackgroundColor)
	assert.Equal(t, "#333333", config.TextColor)
	assert.True(t, config.Enabled)
}

// TestUserRole_TableName 测试用户角色表名
func TestUserRole_TableName(t *testing.T) {
	ur := UserRole{}
	assert.Equal(t, "user_roles", ur.TableName())
}

// TestWebhook_TableName 测试 Webhook 表名
func TestWebhook_TableName(t *testing.T) {
	w := Webhook{}
	assert.Equal(t, "webhooks", w.TableName())
}

// TestWebhookDelivery_TableName 测试 Webhook 投递表名
func TestWebhookDelivery_TableName(t *testing.T) {
	wd := WebhookDelivery{}
	assert.Equal(t, "webhook_deliveries", wd.TableName())
}

// timePtr 辅助函数
func timePtr(t time.Time) *time.Time {
	return &t
}
