from rest_framework import serializers


class VideoGenerateSerializer(serializers.Serializer):
    prompt = serializers.CharField(required=False, allow_blank=True, default='')
    mode = serializers.ChoiceField(choices=['universal', 'keyframe'])
    model = serializers.ChoiceField(choices=['seedance_2.0', 'seedance_2.0_fast'])
    # 显式枚举拒绝 adaptive（火山默认值）— 估算/计费需要明确宽高
    aspect_ratio = serializers.ChoiceField(choices=['16:9', '9:16', '4:3', '1:1', '3:4', '21:9'])
    duration = serializers.IntegerField()
    # 1080p 仅 Seedance 2.0 支持，Fast 不支持 — 上层 video_generate_view 会做 model/resolution 组合校验
    resolution = serializers.ChoiceField(choices=['480p', '720p', '1080p'], required=False, default='720p')
    references = serializers.ListField(child=serializers.DictField(), required=False, default=list)


class QuotaUpdateSerializer(serializers.Serializer):
    daily_generation_limit = serializers.IntegerField(min_value=-1)
    monthly_generation_limit = serializers.IntegerField(min_value=-1)
    spending_limit = serializers.DecimalField(max_digits=12, decimal_places=2, required=False)


class UserStatusSerializer(serializers.Serializer):
    is_active = serializers.BooleanField()


class AdminCreateUserSerializer(serializers.Serializer):
    username = serializers.CharField(max_length=150)
    email = serializers.EmailField()
    password = serializers.CharField(min_length=6)
    daily_seconds_limit = serializers.IntegerField(min_value=-1, required=False, default=600)
    monthly_seconds_limit = serializers.IntegerField(min_value=-1, required=False, default=6000)
    daily_generation_limit = serializers.IntegerField(min_value=-1, required=False, default=50)
    monthly_generation_limit = serializers.IntegerField(min_value=-1, required=False, default=1500)
    is_staff = serializers.BooleanField(required=False, default=False)


class SystemSettingsSerializer(serializers.Serializer):
    default_daily_seconds_limit = serializers.IntegerField(min_value=0, required=False)
    default_monthly_seconds_limit = serializers.IntegerField(min_value=0, required=False)
    default_daily_generation_limit = serializers.IntegerField(min_value=0, required=False)
    default_monthly_generation_limit = serializers.IntegerField(min_value=0, required=False)
    base_token_price = serializers.DecimalField(max_digits=10, decimal_places=2, min_value=0, required=False)
    base_token_price_video = serializers.DecimalField(max_digits=10, decimal_places=2, min_value=0, required=False)
    base_token_price_fast = serializers.DecimalField(max_digits=10, decimal_places=2, min_value=0, required=False)
    base_token_price_fast_video = serializers.DecimalField(max_digits=10, decimal_places=2, min_value=0, required=False)
    base_token_price_1080p = serializers.DecimalField(max_digits=10, decimal_places=2, min_value=0, required=False)
    base_token_price_1080p_video = serializers.DecimalField(max_digits=10, decimal_places=2, min_value=0, required=False)
    announcement = serializers.CharField(required=False, allow_blank=True, default='')
    announcement_enabled = serializers.BooleanField(required=False, default=False)
    max_desktop_sessions = serializers.IntegerField(min_value=1, required=False, default=1)
    max_mobile_sessions = serializers.IntegerField(min_value=0, required=False, default=0)
    # 异常检测配置
    anomaly_detection_enabled = serializers.BooleanField(required=False, default=False)
    r1_enabled_default = serializers.BooleanField(required=False, default=True)
    r2_enabled_default = serializers.BooleanField(required=False, default=True)
    r2_window_seconds = serializers.IntegerField(min_value=60, required=False, default=3600)
    r3_enabled_default = serializers.BooleanField(required=False, default=True)
    r3_window_seconds = serializers.IntegerField(min_value=60, required=False, default=3600)
    r3_max_count = serializers.IntegerField(min_value=1, required=False, default=10)
    r4_enabled_default = serializers.BooleanField(required=False, default=True)
    r4_window_seconds = serializers.IntegerField(min_value=60, required=False, default=3600)
    r4_city_count = serializers.IntegerField(min_value=1, required=False, default=5)
    r5_enabled_default = serializers.BooleanField(required=False, default=True)
    r5_days = serializers.IntegerField(min_value=1, required=False, default=7)
    r5_country_count = serializers.IntegerField(min_value=1, required=False, default=10)
    feishu_alert_mobiles = serializers.CharField(required=False, allow_blank=True, default='')
    sms_alert_mobiles = serializers.CharField(required=False, allow_blank=True, default='')
    alert_cooldown_seconds = serializers.IntegerField(min_value=0, required=False, default=1800)


# ── Team serializers ──

class TeamCreateSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=100)
    monthly_seconds_limit = serializers.IntegerField(min_value=0, required=False, default=6000)
    daily_member_limit_default = serializers.IntegerField(min_value=0, required=False, default=600)
    markup_percentage = serializers.DecimalField(max_digits=5, decimal_places=2, min_value=0, required=True)
    monthly_spending_limit = serializers.DecimalField(max_digits=12, decimal_places=2, required=False, default=-1)
    daily_member_spending_default = serializers.DecimalField(max_digits=12, decimal_places=2, required=False, default=50)
    max_concurrent_tasks = serializers.IntegerField(min_value=0, required=False, default=5)
    expected_regions = serializers.CharField(max_length=500, required=True)


class TeamUpdateSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=100, required=False)
    monthly_seconds_limit = serializers.IntegerField(min_value=0, required=False)
    daily_member_limit_default = serializers.IntegerField(min_value=0, required=False)
    markup_percentage = serializers.DecimalField(max_digits=5, decimal_places=2, min_value=0, required=False)
    monthly_spending_limit = serializers.DecimalField(max_digits=12, decimal_places=2, required=False)
    daily_member_spending_default = serializers.DecimalField(max_digits=12, decimal_places=2, required=False)
    max_concurrent_tasks = serializers.IntegerField(min_value=0, required=False)
    is_active = serializers.BooleanField(required=False)
    expected_regions = serializers.CharField(max_length=500, required=False, allow_blank=True)


class TeamAnomalyConfigSerializer(serializers.Serializer):
    r1_enabled = serializers.BooleanField(required=False, allow_null=True, default=None)
    r2_enabled = serializers.BooleanField(required=False, allow_null=True, default=None)
    r2_window_seconds = serializers.IntegerField(min_value=60, required=False, allow_null=True, default=None)
    r3_enabled = serializers.BooleanField(required=False, allow_null=True, default=None)
    r3_window_seconds = serializers.IntegerField(min_value=60, required=False, allow_null=True, default=None)
    r3_max_count = serializers.IntegerField(min_value=1, required=False, allow_null=True, default=None)
    r4_enabled = serializers.BooleanField(required=False, allow_null=True, default=None)
    r4_window_seconds = serializers.IntegerField(min_value=60, required=False, allow_null=True, default=None)
    r4_city_count = serializers.IntegerField(min_value=1, required=False, allow_null=True, default=None)
    r5_enabled = serializers.BooleanField(required=False, allow_null=True, default=None)
    r5_days = serializers.IntegerField(min_value=1, required=False, allow_null=True, default=None)
    r5_country_count = serializers.IntegerField(min_value=1, required=False, allow_null=True, default=None)


class TeamTopUpSerializer(serializers.Serializer):
    amount = serializers.DecimalField(max_digits=12, decimal_places=2, min_value=0.01)


class TeamAdminCreateSerializer(serializers.Serializer):
    """Create a team admin account for a specific team."""
    username = serializers.CharField(max_length=150)
    email = serializers.EmailField()
    password = serializers.CharField(min_length=6)


class TeamMemberCreateSerializer(serializers.Serializer):
    """Team admin creates a member."""
    username = serializers.CharField(max_length=150)
    password = serializers.CharField(min_length=6)
    daily_seconds_limit = serializers.IntegerField(min_value=-1, required=False)
    monthly_seconds_limit = serializers.IntegerField(min_value=-1, required=False)
    daily_generation_limit = serializers.IntegerField(min_value=-1, required=False)
    monthly_generation_limit = serializers.IntegerField(min_value=-1, required=False)


class MemberQuotaSerializer(serializers.Serializer):
    daily_generation_limit = serializers.IntegerField(min_value=-1)
    monthly_generation_limit = serializers.IntegerField(min_value=-1)
    spending_limit = serializers.DecimalField(max_digits=12, decimal_places=2, required=False)
