import type { FaqItem, PublicSocialLink } from './public'

export interface AdminMetric {
  label: string
  value: number
  note: string
}

export interface AdminActivity {
  label: string
  age: string
}

export interface AdminTopCategory {
  category: string
  value: number
  basis: string
}

export interface AdminRiskFlag {
  id: number
  source: string
  type: string
  severity: string
  status: string
  summary: string
  twitchUserId: string | null
  createdFromIp: string
  createdAt: string
  metadataJson: string
  reviewNote: string | null
  reviewedByTwitchId: string | null
  reviewedAt: string | null
  entityLinks: AdminRiskEntityLink[]
}

export interface AdminRiskEntityLink {
  label: string
  entityType: string
  entityId: string
  to: string
}

export interface AdminRiskCount {
  key: string
  count: number
}

export interface AdminRiskFlagsResponse {
  items: AdminRiskFlag[]
  totalCount: number
  returnedCount: number
  offset: number
  limit: number
  hasMore: boolean
  severityCounts: AdminRiskCount[]
  statusCounts: AdminRiskCount[]
}

export interface AdminRiskQueryOptions {
  limit?: number
  offset?: number
  status?: string
  severity?: string
  query?: string
  reviewedOnly?: boolean
}

export interface AdminRiskRule {
  key: string
  label: string
  enabled: boolean
  threshold: number
  windowMinutes: number
  severity: string
  description: string
}

export interface AdminRiskRulesResponse {
  rules: AdminRiskRule[]
}

export interface AdminWorkflowRule {
  key: string
  label: string
  enabled: boolean
  limit: number
  mode: 'warn' | 'block' | string
  description: string
}

export interface AdminWorkflowRulesResponse {
  rules: AdminWorkflowRule[]
}

export interface AdminTrackingSource {
  providerKey: string
  providerLabel: string
  baseUrl: string
  notesSummary: string
  showManualReviewNotesInReview: boolean
}

export interface AdminTrackingMetricRule {
  key: string
  label: string
  enabled: boolean
  sourceSupport: 'auto' | 'manual' | 'context_only' | string
  description: string
  requiredForAutoClassification: boolean
  showInReview: boolean
  showInAdminSummary: boolean
  manualOverrideAllowed: boolean
  windowKey: '7d' | '30d' | '90d' | 'all_time' | string
  autoSupportedWindowKeys: string[]
  providerFieldKey: string | null
  topCount: number | null
  minPrimaryCategorySharePercent: number | null
  minPrimaryCategoryHours: number | null
  maxDistinctCategoriesBeforeFlag: number | null
  ignoredCategories: string[]
  matchAwardCategoryAgainstTopCategories: boolean
  flagIfAwardCategoryNotInTopX: boolean
  flagIfCategorySpreadTooWide: boolean
  flagIfNoCategoryContextAvailable: boolean
  minValue: number | null
  maxValue: number | null
}

export interface AdminTrackingFlagRule {
  key: string
  label: string
  enabled: boolean
  severity: 'high' | 'medium' | 'low' | string
  description: string
  autoTriggerEnabled: boolean
  requiresManualReview: boolean
  blocksApproval: boolean
  adminNoteRequiredOnOverride: boolean
}

export interface AdminTrackingFlagHit {
  key: string
  label: string
  severity: string
  description: string
  requiresManualReview: boolean
  blocksApproval: boolean
  adminNoteRequiredOnOverride: boolean
}

export interface AdminTrackingMetricState {
  key: string
  label: string
  required: boolean
  sourceSupport: string
  present: boolean
  value: string
  description: string
  windowKey: string
  windowLabel: string
  autoWindowSupported: boolean
}

export interface AdminTrackingRulesResponse {
  source: AdminTrackingSource
  importantMetrics: AdminTrackingMetricRule[]
  optionalMetrics: AdminTrackingMetricRule[]
  flags: AdminTrackingFlagRule[]
  manualReviewNotes: string
}

export interface AdminAuditEntry {
  id: number
  adminTwitchUserId: string
  actionType: string
  entityType: string
  entityId: string
  summary: string
  createdAt: string
  metadataJson: string
  createdFromIp: string | null
  userAgent: string
}

export interface AdminAuditEntriesResponse {
  items: AdminAuditEntry[]
  totalCount: number
  returnedCount: number
  nextCursor: string | null
  limit: number
}

export interface AdminAuditQueryOptions {
  limit?: number
  query?: string
  admin?: string
  action?: string
  entityType?: string
  from?: string
  to?: string
  cursor?: string | null
}

export interface AdminDashboardResponse {
  seasonId: number
  year: number
  seasonName: string
  isCurrent: boolean
  metrics: AdminMetric[]
  activities: AdminActivity[]
  topCategories: AdminTopCategory[]
  riskFlags: AdminRiskFlag[]
  auditEntries: AdminAuditEntry[]
}

export interface DatabaseHealthResponse {
  provider: string
  canConnect: boolean
  pendingMigrations: string[]
  configuredConnection: {
    source: string
  }
  error?: string | null
}

export interface AdminSeasonListItem {
  id: number
  year: number
  name: string
  currentPhase: string
  isCurrent: boolean
  isDemo: boolean
  categoryCount: number
  winnersPublishedAt: string | null
  winnersPublishedByTwitchId: string | null
}

export interface AdminCategoryItem {
  id: number
  groupName: string
  name: string
  slug: string
  description: string
  sortOrder: number
  maxNomineesPerUser: number
  viewerRangeMin: number | null
  viewerRangeMax: number | null
  candidateCount: number
}

export interface AdminSubcategoryTemplate {
  name: string
  slug: string
  sortOrder: number
  viewerRangeMin: number | null
  viewerRangeMax: number | null
}

export interface AdminCandidateItem {
  id: number
  categoryId: number
  streamerIdentityId: number | null
  displayName: string
  channelSlug: string
  platform: string
  avgViewers: number | null
  votes: number
  nominationTally: number
  acceptanceStatus: string
  acceptanceNote: string | null
  clipCompilationUrl: string | null
  clipCompilationTitle: string | null
  clipCompilationPlatform: string | null
  clipEmbedStatus: string
}

export interface AdminCandidateDeletePreview {
  candidateId: number
  nominationCount: number
  clipCount: number
  voteCount: number
  resultCount: number
}

export interface AdminAwardResultItem {
  id: number
  categoryId: number
  categoryName: string
  candidateId: number
  streamerIdentityId: number | null
  candidateDisplayName: string
  candidateChannelSlug: string
  candidatePlatform: string
}

export interface AdminVotingWorkspaceSummary {
  totalVotes: number
  totalSubcategories: number
  votedSubcategories: number
  readySubcategories: number
  problemSubcategories: number
  winnerSetSubcategories: number
}

export interface AdminVotingCandidateRank {
  candidateId: number
  displayName: string
  channelSlug: string
  platform: string
  votes: number
  voteSharePercent: number
  nominationTally: number
  hasClip: boolean
  clipEmbedStatus: string
  hasWinnerConflict: boolean
  isCurrentWinner: boolean
  isAccepted: boolean
  isTopTie: boolean
}

export interface AdminVotingCategoryWorkspaceItem {
  categoryId: number
  groupName: string
  categoryName: string
  sortOrder: number
  viewerRangeMin: number | null
  viewerRangeMax: number | null
  voteCount: number
  candidateCount: number
  readyCandidateCount: number
  nominationCount: number
  openReviewCount: number
  hasWinner: boolean
  winnerReady: boolean
  hasTopVoteTie: boolean
  hasMissingClip: boolean
  hasRuleConflict: boolean
  hasOpenReviews: boolean
  hasSoftNominatorWarning: boolean
  leaderboard: AdminVotingCandidateRank[]
}

export interface AdminVotingWorkspace {
  summary: AdminVotingWorkspaceSummary
  categories: AdminVotingCategoryWorkspaceItem[]
}

export interface AdminNominationReviewItem {
  id: number
  categoryId: number | null
  categoryGroupName: string
  categoryName: string
  submittedByTwitchId: string
  candidateText: string
  streamUrl: string | null
  resolvedChannel: string | null
  resolvedPlatform: string | null
  avgViewers: number | null
  suggestedCategoryId: number | null
  suggestedCategoryName: string | null
  streamerIdentityId: number | null
  trackerStatus: string
  trackerCheckedAt: string | null
  trackingReviewStatus: string
  requiresManualReview: boolean
  trackingFlags: AdminTrackingFlagHit[]
  trackingMetrics: AdminTrackingMetricState[]
  trackingReviewNote: string | null
  trackingReviewedByTwitchId: string | null
  trackingReviewedAt: string | null
  status: string
  createdAt: string
  candidateId: number | null
  candidateDisplayName: string | null
  reviewNote: string | null
  reviewedByTwitchId: string | null
  reviewedAt: string | null
}

export interface AdminNominationReviewGroup {
  id: number
  nominationIds: number[]
  categoryGroupName: string
  displayName: string
  streamUrl: string | null
  resolvedChannel: string | null
  resolvedPlatform: string | null
  avgViewers: number | null
  suggestedCategoryId: number | null
  suggestedCategoryName: string | null
  streamerIdentityId: number | null
  trackerStatus: string
  trackerCheckedAt: string | null
  trackingReviewStatus: string
  requiresManualReview: boolean
  trackingFlags: AdminTrackingFlagHit[]
  trackingMetrics: AdminTrackingMetricState[]
  trackingReviewNote: string | null
  trackingReviewedByTwitchId: string | null
  trackingReviewedAt: string | null
  nominationTally: number
  uniqueSubmitterCount: number
  firstSubmittedAt: string
  lastSubmittedAt: string
}

export interface AdminNominationLinkBlacklistEntry {
  url: string
}

export interface AdminNominationLinkBlacklistResponse {
  entries: AdminNominationLinkBlacklistEntry[]
}

export interface AdminClipSubmissionItem {
  id: number
  categoryId: number | null
  candidateId: number | null
  submittedByTwitchId: string
  clipUrl: string
  title: string
  creator: string
  platform: string
  status: string
  createdAt: string
  reviewNote: string | null
  reviewedByTwitchId: string | null
  reviewedAt: string | null
}

export interface AdminSeasonDetailResponse {
  id: number
  year: number
  name: string
  isDemo: boolean
  currentPhase: string
  isCurrent: boolean
  isCommunityOnly: boolean
  nominationStartsAt: string
  nominationEndsAt: string
  votingStartsAt: string
  votingEndsAt: string
  reviewStartsAt: string
  reviewEndsAt: string
  showDate: string
  showStartsAt: string
  winnersPublishedAt: string | null
  winnersPublishedByTwitchId: string | null
  subcategoryTemplates: AdminSubcategoryTemplate[]
  categories: AdminCategoryItem[]
  candidates: AdminCandidateItem[]
  pendingNominations: AdminNominationReviewItem[]
  pendingNominationGroups: AdminNominationReviewGroup[]
  reviewedNominations: AdminNominationReviewItem[]
  trackingReviewNotes: string
  showTrackingReviewNotes: boolean
  results: AdminAwardResultItem[]
  votingWorkspace: AdminVotingWorkspace
  clipSubmissions: AdminClipSubmissionItem[]
}

export interface AdminSiteSettingsResponse {
  hostDisplayName: string
  hostTagline: string
  hostArtistName: string
  hostImageUrl: string
  newsletterUrl: string
  shareXUrl: string
  shareDiscordUrl: string
  privacyEmail: string
  privacyPolicyContent: string
  privacyPolicyUpdatedBy: string | null
  privacyPolicyUpdatedAt: string | null
  imprintUrl: string
  imprintContent: string
  contactUrl: string
  contactContent: string
  sponsorsUrl: string
  sponsorsContent: string
  showactsUrl: string
  showactsContent: string
  streamBannerEyebrow: string
  streamBannerTitle: string
  streamBannerText: string
  streamBannerLiveButtonLabel: string
  streamBannerLiveButtonUrl: string
  streamBannerLockedButtonLabel: string
  streamBannerUseCompletedContent: boolean
  streamBannerCompletedEyebrow: string
  streamBannerCompletedTitle: string
  streamBannerCompletedText: string
  streamBannerCompletedButtonLabel: string
  streamBannerCompletedButtonUrl: string
  awardsSectionTitle: string
  awardsSectionDescription: string
  subcategoriesSectionTitle: string
  subcategoriesSectionDescription: string
  socialLinks: PublicSocialLink[]
  faq: FaqItem[]
  showactFormSchemaJson: string
}

export interface AdminOperationalSettingsResponse {
  demoLoginManagedByDatabase: boolean
  demoLoginEnabled: boolean
  demoLoginEmail: string
  demoLoginPasswordSet: boolean
  demoLoginTwitchUserId: string
  demoLoginDisplayName: string
  twitchAuthManagedByDatabase: boolean
  twitchAuthConfigured: boolean
  twitchClientId: string
  twitchClientSecretSet: boolean
  twitchRedirectUri: string
  twitchScope: string
  sessionIdleTimeoutHours: number
  maintenanceModeEnabled: boolean
  maintenanceTitle: string
  maintenanceMessage: string
}

export interface AdminOptionalFeatureSettingsResponse {
  clipSubmissionsEnabled: boolean
  clipReviewEnabled: boolean
  clipAdminMenuVisible: boolean
  clipSubmissionDisabledMessage: string
  showactApplicationsEnabled: boolean
  showactApplicationStartsAt: string | null
  showactApplicationEndsAt: string | null
  showactApplicationsOpenNow: boolean
  showactApplicationDisabledMessage: string
  sponsorsVisible: boolean
}

export interface AdminSponsorItem {
  id: number
  seasonId: number
  name: string
  websiteUrl: string
  logoUrl: string
  description: string
  tier: string
  sortOrder: number
  isVisible: boolean
}

export interface AdminArchivedWinnerItem {
  id: number
  year: number
  category: string
  subcategory: string
  winnerName: string
  winnerUrl: string
  createdAt: string
  updatedAt: string | null
}

export interface AdminShowactApplicationItem {
  id: number
  seasonId: number
  artistName: string
  contactEmail: string
  contactDiscord: string
  platformUrl: string
  performanceType: string
  description: string
  technicalNotes: string
  referenceUrl: string
  status: string
  reviewNote: string | null
  createdAt: string
  reviewedAt: string | null
  fieldResponsesJson: string
}

export interface AdminTeamPermission {
  key: string
  label: string
  description: string
  groupLabel: string
  menuPath: string
  readOnlySupported: boolean
}

export interface AdminTeamRole {
  key: string
  label: string
  description: string
  isSystemRole: boolean
  permissionKeys: string[]
}

export interface AdminTeamMember {
  id: number
  login: string
  displayName: string
  role: string
  boundTwitchUserId: string | null
  boundTwitchDisplayName: string | null
  isActive: boolean
  mustChangePassword: boolean
  createdAt: string
  updatedAt: string | null
  lastLoginAt: string | null
  lastOnlineAt: string | null
  isOnline: boolean
  twitchBoundAt: string | null
  passwordResetAt: string | null
}

export interface AdminTeamResponse {
  members: AdminTeamMember[]
  roles: AdminTeamRole[]
  permissions: AdminTeamPermission[]
}

export interface CreateTeamMemberPayload {
  login: string
  displayName: string
  role: string
}

export interface UpdateTeamMemberPayload {
  login: string
  displayName: string
  role: string
  isActive: boolean
}

export interface UpdateTeamRolesPayload {
  roles: Array<{
    key: string
    permissionKeys: string[]
  }>
}

export interface TeamMemberPasswordResponse {
  saved: boolean
  memberId: number
  generatedPassword: string
  mustChangePassword: boolean
}
