import type {
  Competition,
  Round,
  JuryGroup,
  JuryGroupMember,
  SubmissionWindow,
  SubmissionFileRequirement,
  RoundSubmissionVisibility,
  ProjectRoundState,
  DeliberationSession,
  ResultLock,
  RoundType,
  CompetitionStatus,
  RoundStatus,
  CapMode,
  JuryGroupMemberRole,
} from '@prisma/client'
import type { RoundConfigMap } from './competition-configs'

// ============================================================================
// Composite Types (models with nested relations)
// ============================================================================

export type CompetitionWithRounds = Competition & {
  rounds: RoundSummary[]
  juryGroups: JuryGroupSummary[]
  submissionWindows: SubmissionWindowSummary[]
}

export type RoundSummary = Pick<
  Round,
  'id' | 'name' | 'slug' | 'roundType' | 'status' | 'sortOrder' | 'windowOpenAt' | 'windowCloseAt'
>

export type RoundWithRelations = Round & {
  juryGroup: (JuryGroup & { members: JuryGroupMember[] }) | null
  submissionWindow: (SubmissionWindow & { fileRequirements: SubmissionFileRequirement[] }) | null
  visibleSubmissionWindows: (RoundSubmissionVisibility & { submissionWindow: SubmissionWindow })[]
  _count?: { projectRoundStates: number; assignments: number }
}

export type JuryGroupSummary = Pick<
  JuryGroup,
  'id' | 'name' | 'slug' | 'sortOrder' | 'defaultMaxAssignments' | 'defaultCapMode'
> & {
  _count: { members: number }
}

export type JuryGroupWithMembers = JuryGroup & {
  members: (JuryGroupMember & {
    user: { id: string; name: string | null; email: string; role: string }
  })[]
}

export type SubmissionWindowSummary = Pick<
  SubmissionWindow,
  'id' | 'name' | 'slug' | 'roundNumber' | 'windowOpenAt' | 'windowCloseAt' | 'isLocked'
> & {
  _count: { fileRequirements: number; projectFiles: number }
}

// ============================================================================
// Policy Resolution
// ============================================================================

export type PolicySource =
  | 'system'
  | 'program'
  | 'jury_group'
  | 'member'
  | 'admin_override'

export type PolicyResolution<T> = {
  value: T
  source: PolicySource
  explanation: string
}

// ============================================================================
// tRPC Input Types
// ============================================================================

export type CreateCompetitionInput = {
  programId: string
  name: string
  slug: string
  categoryMode?: string
  startupFinalistCount?: number
  conceptFinalistCount?: number
  notifyOnRoundAdvance?: boolean
  notifyOnDeadlineApproach?: boolean
  deadlineReminderDays?: number[]
}

export type UpdateCompetitionInput = {
  id: string
  name?: string
  slug?: string
  status?: CompetitionStatus
  categoryMode?: string
  startupFinalistCount?: number
  conceptFinalistCount?: number
  notifyOnRoundAdvance?: boolean
  notifyOnDeadlineApproach?: boolean
  deadlineReminderDays?: number[]
}

export type CreateRoundInput = {
  competitionId: string
  name: string
  slug: string
  roundType: RoundType
  sortOrder: number
  configJson?: Record<string, unknown>
  windowOpenAt?: Date | null
  windowCloseAt?: Date | null
  juryGroupId?: string | null
  submissionWindowId?: string | null
  purposeKey?: string | null
}

export type UpdateRoundInput = {
  id: string
  name?: string
  slug?: string
  status?: RoundStatus
  configJson?: Record<string, unknown>
  windowOpenAt?: Date | null
  windowCloseAt?: Date | null
  juryGroupId?: string | null
  submissionWindowId?: string | null
  purposeKey?: string | null
}

export type CreateJuryGroupInput = {
  competitionId: string
  name: string
  slug: string
  description?: string
  sortOrder?: number
  defaultMaxAssignments?: number
  defaultCapMode?: CapMode
  softCapBuffer?: number
  categoryQuotasEnabled?: boolean
  defaultCategoryQuotas?: Record<string, { min: number; max: number }>
  allowJurorCapAdjustment?: boolean
  allowJurorRatioAdjustment?: boolean
}

export type AddJuryGroupMemberInput = {
  juryGroupId: string
  userId: string
  role?: JuryGroupMemberRole
  maxAssignmentsOverride?: number | null
  capModeOverride?: CapMode | null
  categoryQuotasOverride?: Record<string, { min: number; max: number }> | null
  preferredStartupRatio?: number | null
  availabilityNotes?: string | null
}

export type UpdateJuryGroupMemberInput = {
  id: string
  role?: JuryGroupMemberRole
  maxAssignmentsOverride?: number | null
  capModeOverride?: CapMode | null
  categoryQuotasOverride?: Record<string, { min: number; max: number }> | null
  preferredStartupRatio?: number | null
  availabilityNotes?: string | null
}
