// Admin panel types - aligned with backend schemas/admin.py

export interface DashboardMetrics {
  total_users: number
  active_subscriptions: number
  paid_accounts: number
  total_trees: number
}

export interface ActivityEntry {
  id: string
  user_email: string | null
  action: string
  resource_type: string
  resource_id: string | null
  details: Record<string, unknown> | null
  ip_address: string | null
  created_at: string
}

export interface AuditLogEntry {
  id: string
  user_id: string
  user_email: string | null
  action: string
  resource_type: string
  resource_id: string | null
  details: Record<string, unknown> | null
  ip_address: string | null
  created_at: string
}

export interface AuditLogListResponse {
  items: AuditLogEntry[]
  total: number
  page: number
  per_page: number
}

export interface PlanLimitConfig {
  plan: string
  max_trees: number | null
  max_sessions_per_month: number | null
  max_users: number | null
  custom_branding: boolean
  priority_support: boolean
  export_formats: string[]
}

export interface AccountOverrideResponse {
  id: string
  account_id: string
  account_name: string | null
  account_display_code: string | null
  override_max_trees: number | null
  override_max_sessions_per_month: number | null
  override_max_users: number | null
  note: string | null
  created_at: string
  updated_at: string
}

export interface PlanDefaultEntry {
  plan: string
  enabled: boolean
}

export interface FeatureFlagResponse {
  id: string
  flag_key: string
  display_name: string
  description: string | null
  plan_defaults: PlanDefaultEntry[]
  created_at: string
}

export interface AccountFeatureOverrideResponse {
  id: string
  account_id: string
  account_display_code: string | null
  flag_id: string
  flag_key: string | null
  flag_display_name: string | null
  enabled: boolean
  note: string | null
  created_at: string
}

export interface AdminCategory {
  id: string
  name: string
  slug: string
  description: string | null
  account_id: string | null
  tree_count: number
}

// Request types
export interface AccountOverrideCreate {
  account_display_code: string
  override_max_trees?: number | null
  override_max_sessions_per_month?: number | null
  override_max_users?: number | null
  note?: string | null
}

export interface FeatureFlagCreate {
  flag_key: string
  display_name: string
  description?: string | null
}

export interface PlanDefaultUpdate {
  plan: string
  flag_id: string
  enabled: boolean
}

export interface AccountFeatureOverrideCreate {
  account_display_code: string
  flag_id: string
  enabled: boolean
  note?: string | null
}

export interface GlobalCategoryCreate {
  name: string
  slug: string
  description?: string | null
}

// Invite code types (enhanced)
export interface InviteCodeResponse {
  id: string
  code: string
  created_by_id: string
  used_by_id: string | null
  expires_at: string | null
  note: string | null
  created_at: string
  used_at: string | null
  is_used: boolean
  is_expired: boolean
  is_valid: boolean
  email: string | null
  assigned_plan: string
  trial_duration_days: number | null
  email_sent_at: string | null
  has_trial: boolean
  email_sent: boolean
}

export interface InviteCodeCreateRequest {
  expires_at?: string | null
  note?: string | null
  email?: string | null
  assigned_plan?: 'free' | 'pro' | 'team'
  trial_duration_days?: number | null
}

// Admin user creation types
export interface AdminUserCreate {
  email: string
  name: string
  account_mode: 'existing' | 'personal'
  account_display_code?: string
  account_role?: 'engineer' | 'viewer'
  send_email: boolean
}

export interface AdminUserCreateResponse {
  user: Record<string, unknown>
  temporary_password: string
  email_sent: boolean
}

// User detail types
export interface AccountSummary {
  id: string
  name: string
  display_code: string | null
}

export interface SubscriptionSummary {
  id: string
  plan: string
  status: string
  current_period_start: string | null
  current_period_end: string | null
}

export interface SessionSummary {
  id: string
  tree_name: string | null
  started_at: string
  completed_at: string | null
  outcome: string | null
}

export interface AuditLogSummary {
  id: string
  action: string
  resource_type: string | null
  resource_id: string | null
  created_at: string
  details: Record<string, unknown> | null
}

export interface InviteCodeUsedSummary {
  code: string
  assigned_plan: string
  trial_duration_days: number | null
  created_by_email: string | null
}

export interface UserDetailResponse {
  id: string
  email: string
  full_name: string | null
  role: string
  is_active: boolean
  is_super_admin: boolean
  is_team_admin: boolean
  created_at: string
  deleted_at: string | null
  account: AccountSummary | null
  subscription: SubscriptionSummary | null
  invite_code_used: InviteCodeUsedSummary | null
  recent_sessions: SessionSummary[]
  total_sessions: number
  recent_audit_logs: AuditLogSummary[]
  total_audit_logs: number
}
