from django.db import models


class SystemSettings(models.Model):
    goedge_base_url = models.URLField(blank=True, default='')
    # 令牌与认证
    edge_access_token = models.CharField(max_length=255, blank=True, default='')
    edge_token_expires_at = models.DateTimeField(null=True, blank=True)
    admin_access_key_id = models.CharField(max_length=64, blank=True, default='')
    admin_access_key = models.CharField(max_length=128, blank=True, default='')
    # 默认节点集群（用于创建网站时的 nodeClusterId）
    default_node_cluster_id = models.BigIntegerField(null=True, blank=True)
    default_free_traffic_gb_per_domain = models.PositiveIntegerField(default=15)
    default_overage_policy = models.JSONField(default=dict, blank=True)
    cname_template = models.CharField(max_length=255, blank=True, default='{sub}.cdn.example.com')
    # 默认策略/资源引用（用于域名功能同步）
    default_http_access_log_policy_id = models.BigIntegerField(null=True, blank=True)
    default_http_firewall_policy_id = models.BigIntegerField(null=True, blank=True)
    # 异常流量检测配置
    anomaly_detection_enabled = models.BooleanField(default=True)
    anomaly_threshold_multiplier = models.FloatField(default=3.0)
    anomaly_window_days = models.PositiveIntegerField(default=7)
    anomaly_min_gb = models.FloatField(default=1.0)
    # 简单图形验证码开关（登录/注册）
    captcha_enabled = models.BooleanField(default=False)
    epay_api_base_url = models.URLField(blank=True, default='')
    epay_pid = models.CharField(max_length=64, blank=True, default='')
    epay_key = models.CharField(max_length=128, blank=True, default='')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '系统设置'
        verbose_name_plural = '系统设置'


class OperationLog(models.Model):
    actor = models.ForeignKey('auth.User', null=True, blank=True, on_delete=models.SET_NULL)
    action = models.CharField(max_length=100)
    target = models.CharField(max_length=200, blank=True, default='')
    detail = models.TextField(blank=True, default='')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = '操作日志'
        verbose_name_plural = '操作日志'
        ordering = ['-created_at']

# Create your models here.
