export type CreationMode = 'universal' | 'keyframe';
export type ModelOption = 'seedance_2.0' | 'seedance_2.0_fast';
export type AspectRatio = '16:9' | '9:16' | '1:1' | '21:9' | '4:3' | '3:4';
export type Duration = 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15;
export type Resolution = '480p' | '720p' | '1080p';
export type GenerationType = 'video' | 'image';
export type UserRole = 'super_admin' | 'team_admin' | 'member';

export interface UploadedFile {
  id: string;
  file?: File;
  type: 'image' | 'video' | 'audio';
  previewUrl: string;
  label: string;
  tosUrl?: string; // TOS URL after upload
  uploading?: boolean;
  uploadError?: boolean;
  duration?: number; // media duration in seconds (audio/video only)
}

export interface DropdownOption<T = string> {
  label: string;
  value: T;
  icon?: string;
  disabled?: boolean;
}

export type TaskStatus = 'generating' | 'completed' | 'failed';

export interface ReferenceSnapshot {
  id: string;
  type: 'image' | 'video' | 'audio';
  previewUrl: string;
  label: string;
  role?: string;
  isAssetRef?: boolean;
}

export interface GenerationTask {
  id: string;
  taskId: string; // backend UUID task_id
  prompt: string;
  editorHtml: string;
  mode: CreationMode;
  model: ModelOption;
  aspectRatio: AspectRatio;
  duration: Duration;
  resolution: Resolution;
  references: ReferenceSnapshot[];
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  assetMentions: Record<string, any>[];
  status: TaskStatus;
  progress: number;
  resultUrl?: string;
  thumbnailUrl?: string;
  errorMessage?: string;
  createdAt: number;
  tokensConsumed?: number;
  costAmount?: number;
  isFavorited?: boolean;
  seed?: number;
}

export interface BackendTask {
  id: number;
  task_id: string;
  ark_task_id: string;
  prompt: string;
  mode: CreationMode;
  model: ModelOption;
  aspect_ratio: string;
  resolution: Resolution;
  duration: number;
  seconds_consumed: number;
  tokens_consumed: number;
  cost_amount: number;
  base_cost_amount: number;
  status: 'queued' | 'processing' | 'completed' | 'failed';
  result_url: string;
  thumbnail_url: string;
  error_message: string;
  reference_urls: { url: string; type: string; role: string; label: string; thumb_url?: string }[];
  is_favorited: boolean;
  seed: number;
  created_at: string;
}

// Auth types
export interface User {
  id: number;
  username: string;
  email: string;
  is_staff: boolean;
  is_team_admin: boolean;
  is_team_owner?: boolean;
  is_observer?: boolean;
  role: UserRole;
  team_name: string | null;
  must_change_password: boolean;
}

export interface TeamInfo {
  id: number;
  name: string;
  total_seconds_pool: number;
  total_seconds_used: number;
  remaining_seconds: number;
  monthly_seconds_limit: number;
  monthly_seconds_used: number;
  balance: number;
  total_spent: number;
  available_balance: number;
  monthly_spending_limit: number;
  monthly_spent: number;
  frozen_amount: number;
  token_price: number;
  token_price_video: number;
  token_price_fast: number;
  token_price_fast_video: number;
  token_price_1080p: number;
  token_price_1080p_video: number;
  is_active: boolean;
}

// Phase 3: seconds-based quota
export interface Quota {
  daily_seconds_limit: number;
  daily_seconds_used: number;
  monthly_seconds_limit: number;
  monthly_seconds_used: number;
  daily_generation_limit: number;
  daily_generation_used: number;
  monthly_generation_limit: number;
  monthly_generation_used: number;
}

export interface AuthTokens {
  access: string;
  refresh: string;
}

// Admin types
export interface AdminStats {
  total_users: number;
  total_teams: number;
  new_users_today: number;
  seconds_consumed_today: number;
  seconds_consumed_this_month: number;
  cost_today: number;
  cost_this_month: number;
  base_cost_today: number;
  base_cost_this_month: number;
  total_revenue: number;
  total_base_cost: number;
  total_profit: number;
  profit_margin: number;
  today_change_percent: number;
  month_change_percent: number;
  daily_trend: { date: string; seconds: number; cost: number; base_cost: number }[];
  top_users: { user_id: number; username: string; seconds_consumed: number; cost_consumed: number }[];
  top_teams: { team_id: number; name: string; seconds_consumed: number; cost_consumed: number }[];
  team_profit_ranking: { team_id: number; name: string; revenue: number; base_cost: number; profit: number; markup_percentage: number }[];
}

export interface AdminUser {
  id: number;
  username: string;
  email: string;
  is_active: boolean;
  disabled_by: string;
  is_staff: boolean;
  is_team_admin: boolean;
  is_team_owner?: boolean;
  is_observer?: boolean;
  team_id: number | null;
  team_name: string | null;
  date_joined: string;
  daily_seconds_limit: number;
  monthly_seconds_limit: number;
  seconds_today: number;
  seconds_this_month: number;
  daily_generation_limit: number;
  monthly_generation_limit: number;
  generations_today: number;
  generations_this_month: number;
  spent_today: number;
  spent_this_month: number;
  spending_limit: number;
  total_spent: number;
  is_online?: boolean;
}

export interface AdminUserDetail extends AdminUser {
  seconds_total: number;
  total_spent: number;
  recent_records: AdminRecord[];
}

export interface AdminRecord {
  id: number;
  created_at: string;
  completed_at?: string | null;
  user_id?: number;
  username?: string;
  team_name?: string;
  seconds_consumed: number;
  tokens_consumed: number;
  cost_amount: number;
  base_cost_amount: number;
  prompt: string;
  mode: CreationMode;
  model: ModelOption;
  aspect_ratio?: string;
  resolution?: Resolution;
  status: 'queued' | 'processing' | 'completed' | 'failed';
  error_message?: string;
  raw_error?: string;
  reference_urls?: Array<{ type?: string; url?: string; name?: string; label?: string; thumb_url?: string; role?: string }>;
  duration?: number;
  seed?: number;
  ark_task_id?: string;
  result_url?: string;
  thumbnail_url?: string;
  api_prompt?: string;
}

export interface SystemSettings {
  default_daily_seconds_limit: number;
  default_monthly_seconds_limit: number;
  default_daily_generation_limit: number;
  default_monthly_generation_limit: number;
  base_token_price: number;
  base_token_price_video: number;
  base_token_price_fast: number;
  base_token_price_fast_video: number;
  base_token_price_1080p: number;
  base_token_price_1080p_video: number;
  announcement: string;
  announcement_enabled: boolean;
  max_desktop_sessions: number;
  max_mobile_sessions: number;
  // Anomaly detection
  anomaly_detection_enabled: boolean;
  r1_enabled_default: boolean;
  r2_enabled_default: boolean;
  r2_window_seconds: number;
  r3_enabled_default: boolean;
  r3_window_seconds: number;
  r3_max_count: number;
  r4_enabled_default: boolean;
  r4_window_seconds: number;
  r4_city_count: number;
  r5_enabled_default: boolean;
  r5_days: number;
  r5_country_count: number;
  feishu_alert_mobiles: string;
  sms_alert_mobiles: string;
  alert_cooldown_seconds: number;
}

export interface ProfileOverview {
  daily_seconds_limit: number;
  daily_seconds_used: number;
  monthly_seconds_limit: number;
  monthly_seconds_used: number;
  total_seconds_used: number;
  daily_generation_limit: number;
  daily_generation_used: number;
  monthly_generation_limit: number;
  monthly_generation_used: number;
  daily_spent: number;
  monthly_spent: number;
  total_spent_amount: number;
  daily_trend: { date: string; seconds: number }[];
  team?: {
    name: string;
    total_seconds_pool: number;
    total_seconds_used: number;
    remaining_seconds: number;
    monthly_seconds_limit: number;
    monthly_seconds_used: number;
    balance: number;
    total_spent: number;
    available_balance: number;
    monthly_spending_limit: number;
    monthly_spent: number;
    frozen_amount: number;
  };
}

export interface PaginatedResponse<T> {
  total: number;
  page: number;
  page_size: number;
  results: T[];
}

// Team management types
export interface Team {
  id: number;
  name: string;
  total_seconds_pool: number;
  total_seconds_used: number;
  remaining_seconds: number;
  monthly_seconds_limit: number;
  monthly_seconds_used: number;
  balance: number;
  total_spent: number;
  available_balance: number;
  monthly_spending_limit: number;
  monthly_spent: number;
  frozen_amount: number;
  markup_percentage: number;
  daily_member_limit_default: number;
  max_concurrent_tasks: number;
  current_processing?: number;
  member_count: number;
  is_active: boolean;
  expected_regions: string;
  disabled_by: string;
  created_at: string;
}

export interface TeamAnomalyConfig {
  r1_enabled: boolean | null;
  r2_enabled: boolean | null;
  r2_window_seconds: number | null;
  r3_enabled: boolean | null;
  r3_window_seconds: number | null;
  r3_max_count: number | null;
  r4_enabled: boolean | null;
  r4_window_seconds: number | null;
  r4_city_count: number | null;
  r5_enabled: boolean | null;
  r5_days: number | null;
  r5_country_count: number | null;
}

export interface TeamDetail extends Team {
  anomaly_config: TeamAnomalyConfig | null;
  members: TeamMember[];
}

export interface TeamMember {
  id: number;
  username: string;
  email: string;
  is_team_admin: boolean;
  is_team_owner?: boolean;
  is_observer?: boolean;
  is_active: boolean;
  disabled_by: string;
  daily_seconds_limit: number;
  monthly_seconds_limit: number;
  seconds_today: number;
  seconds_this_month: number;
  daily_generation_limit: number;
  monthly_generation_limit: number;
  generations_today: number;
  generations_this_month: number;
  spent_today: number;
  spent_this_month: number;
  spending_limit: number;
  total_spent: number;
  is_online?: boolean;
  date_joined: string;
}

export interface LoginAnomaly {
  id: number;
  team_id: number;
  team_name: string;
  user_id: number;
  username: string;
  level: 'warning' | 'critical';
  rule: string;
  detail: Record<string, unknown>;
  alerted: boolean;
  auto_disabled: boolean;
  disabled_target: string;
  ip_address: string;
  geo_country: string;
  geo_province: string;
  geo_city: string;
  created_at: string;
}

export interface TeamStats {
  daily_trend: { date: string; seconds: number; cost?: number }[];
  member_consumption: { user_id: number; username: string; seconds_consumed: number; cost_consumed?: number }[];
}

// Asset management types
export interface AssetTeamSummary {
  id: number;
  name: string;
  video_count: number;
  seconds_consumed: number;
  cost_consumed?: number;
  member_count: number;
  is_active: boolean;
}

export interface AssetMemberSummary {
  id: number;
  username: string;
  is_team_admin: boolean;
  is_team_owner?: boolean;
  video_count: number;
  seconds_consumed: number;
  cost_consumed?: number;
  is_online?: boolean;
}

export interface AssetVideo {
  id: number;
  task_id: string;
  prompt: string;
  result_url: string;
  thumbnail_url?: string;
  duration: number;
  seconds_consumed: number;
  cost_amount?: number;
  aspect_ratio: string;
  resolution: Resolution;
  reference_urls?: { url: string; type: string; role: string; label: string; thumb_url?: string }[];
  created_at: string;
}

export interface AuditLog {
  id: number;
  operator_name: string;
  action: string;
  action_display: string;
  target_type: string;
  target_id: number | null;
  target_name: string;
  before: Record<string, unknown> | null;
  after: Record<string, unknown> | null;
  ip_address: string | null;
  created_at: string;
}

export interface AssetGroup {
  id: number;
  name: string;
  thumbnail_url: string;
  asset_count: number;
  remote_group_id: string;
  description: string;
  created_at: string;
}

export interface AssetItem {
  id: number;
  name: string;
  url: string;
  asset_type: 'Image' | 'Video' | 'Audio';
  thumbnail_url: string;
  duration: number | null;
  status: 'processing' | 'active' | 'failed';
  remote_asset_id: string;
  error_message: string;
  created_at: string;
}

export interface AssetSearchResult {
  id: number;
  name: string;
  url: string;
  asset_type: 'Image' | 'Video' | 'Audio';
  group_name: string;
  remote_asset_id: string;
  thumbnail_url: string;
  duration: number | null;
}

// In-app notifications (站内消息)
export type NotificationType =
  | 'anomaly_disabled_user'
  | 'anomaly_disabled_team'
  | 'quota_warning'
  | 'announcement'
  | 'system';

// 用 AppNotification 命名,避免与浏览器内置的 Notification Web API 冲突
export interface AppNotification {
  id: number;
  type: NotificationType;
  title: string;
  content: string;
  link_url: string;
  is_read: boolean;
  created_at: string;
}

export interface NotificationListResponse {
  total: number;
  unread_count: number;
  page: number;
  page_size: number;
  results: AppNotification[];
}
