"""风控监控后台接口 schema。"""
from __future__ import annotations

from datetime import datetime

from pydantic import BaseModel, Field


class RiskSummaryCard(BaseModel):
    kind: str
    alert_subject_count: int
    today_total: int
    threshold: int
    rule_text: str


class RiskMonitorSummary(BaseModel):
    date: str
    updated_at: datetime
    cards: list[RiskSummaryCard]


class RiskRuleConfig(BaseModel):
    sms_hourly_threshold: int = Field(ge=1, le=5)
    oneclick_daily_threshold: int = Field(ge=1, le=100_000)
    compare_daily_threshold: int = Field(ge=1, le=100)


class RiskIncidentItem(BaseModel):
    incident_id: int
    kind: str
    subject_type: str
    subject_id: str
    device_model: str | None = None
    first_used_at: datetime | None = None
    phone: str | None = None
    user_id: int | None = None
    registered_at: datetime | None = None
    common_device_id: str | None = None
    window_start: datetime
    window_end: datetime
    first_event_at: datetime
    triggered_at: datetime
    last_event_at: datetime
    event_count: int
    status: str
    restricted: bool = False
    restriction_id: int | None = None


class RiskIncidentPage(BaseModel):
    items: list[RiskIncidentItem]
    next_cursor: int | None = None
    total: int


class RiskDetailItem(BaseModel):
    id: int
    occurred_at: datetime
    outcome: str
    phone: str | None = None
    user_id: int | None = None
    username: str | None = None
    account_registered_at: datetime | None = None
    verified: bool | None = None
    reason: str | None = None
    store_name: str | None = None
    product_names: str | None = None
    prices: dict[str, float | None] | None = None
    saved_amount_yuan: float | None = None
    status: str | None = None


class RiskDetailPage(BaseModel):
    kind: str
    incident_id: int
    event_count: int
    distinct_accounts: int | None = None
    items: list[RiskDetailItem]
    next_cursor: int | None = None
    total: int


class RiskActionRequest(BaseModel):
    reason: str = Field("", max_length=256)


class RiskActionResponse(BaseModel):
    ok: bool = True
    incident_id: int
    status: str
    restriction_id: int | None = None


class RiskResetResponse(BaseModel):
    ok: bool = True
    reset_at: datetime
    reset_incident_count: int
    reset_counts: dict[str, int]


class RestrictionRevokeResponse(BaseModel):
    ok: bool = True
    restriction_id: int
