export interface User {
  id: number
  username: string
  email: string
  first_name: string | null
  last_name: string | null
  display_name: string | null
  avatar_url: string | null
  role: 'admin' | 'dak_mitarbeiter'
  mfa_enabled: boolean
  is_active: boolean
  last_login: string | null
  created_at: string
}

export interface LoginRequest {
  email: string
  password: string
  mfa_code?: string
}

export interface RegisterRequest {
  username: string
  email: string
  password: string
  invitation_token?: string
}

export interface TokenResponse {
  access_token: string
  refresh_token: string
  token_type: string
  user: User
}

export interface Case {
  id: number
  fall_id: string | null
  crm_ticket_id: string | null
  jahr: number
  kw: number
  datum: string
  anrede: string | null
  vorname: string | null
  nachname: string | null
  geburtsdatum: string | null
  kvnr: string | null
  versicherung: string
  icd: string | null
  fallgruppe: string
  strasse: string | null
  plz: string | null
  ort: string | null
  email: string | null
  ansprechpartner: string | null
  telefonnummer: string | null
  mobiltelefon: string | null
  unterlagen: boolean
  unterlagen_verschickt: string | null
  erhalten: boolean | null
  unterlagen_erhalten: string | null
  unterlagen_an_gutachter: string | null
  gutachten: boolean
  gutachter: string | null
  gutachten_erstellt: string | null
  gutachten_versendet: string | null
  schweigepflicht: boolean
  ablehnung: boolean
  abbruch: boolean
  abbruch_datum: string | null
  gutachten_typ: string | null
  therapieaenderung: string | null
  ta_diagnosekorrektur: boolean
  ta_unterversorgung: boolean
  ta_uebertherapie: boolean
  kurzbeschreibung: string | null
  fragestellung: string | null
  kommentar: string | null
  sonstiges: string | null
  abgerechnet: boolean
  abrechnung_datum: string | null
  import_source: string | null
  imported_at: string
  updated_at: string
  disclosure_granted?: boolean
  disclosure_expires_at?: string | null
}

export interface CaseListResponse {
  items: Case[]
  total: number
  page: number
  per_page: number
}

export interface DashboardKPIs {
  total_cases: number
  pending_icd: number
  pending_coding: number
  total_gutachten: number
  total_abgerechnet: number
  pending_abrechnung: number
  fallgruppen: Record<string, number>
}

export interface WeeklyDataPoint {
  kw: number
  erstberatungen: number
  unterlagen: number
  ablehnungen: number
  keine_rm: number
  gutachten: number
}

export interface DashboardResponse {
  kpis: DashboardKPIs
  prev_kpis?: DashboardKPIs | null
  weekly: WeeklyDataPoint[]
}

export interface YearlyComparisonPoint {
  kw: number
  [year: string]: number
}

export interface YearlyComparisonResponse {
  data: YearlyComparisonPoint[]
}

export interface TopIcdItem {
  icd: string
  count: number
}

export interface TopIcdResponse {
  items: TopIcdItem[]
}

export interface TopGutachterItem {
  gutachter: string
  count: number
}

export interface TopGutachterResponse {
  items: TopGutachterItem[]
}

export interface GutachtenStatistikKPIs {
  total_gutachten: number
  bestaetigung: number
  alternative: number
  uncodiert: number
  ta_ja: number
  ta_nein: number
  diagnosekorrektur: number
  unterversorgung: number
  uebertherapie: number
}

export interface GutachtenWeeklyPoint {
  kw: number
  bestaetigung: number
  alternative: number
}

export interface TherapieWeeklyPoint {
  kw: number
  gutachten: number
  ta_ja: number
  ta_nein: number
  diagnosekorrektur: number
  unterversorgung: number
  uebertherapie: number
}

export interface GutachtenStatistikResponse {
  kpis: GutachtenStatistikKPIs
  gutachten_weekly: GutachtenWeeklyPoint[]
  therapie_weekly: TherapieWeeklyPoint[]
}

export interface Notification {
  id: number
  notification_type: string
  title: string
  message: string | null
  is_read: boolean
  created_at: string
}

export interface ImportPreview {
  filename: string
  total_rows: number
  new_cases: number
  duplicates: number
  errors: string[]
  rows: ImportRow[]
}

export interface ImportRow {
  row_number: number
  nachname: string
  vorname: string | null
  fallgruppe: string
  datum: string
  is_duplicate: boolean
  fall_id: string | null
}

export interface ImportResult {
  imported: number
  skipped: number
  updated: number
  errors: string[]
}

export interface ReportMeta {
  id: number
  jahr: number
  kw: number
  report_type: string
  report_date: string
  generated_at: string
}

export interface ICDImportResponse {
  updated: number
  errors: string[]
}

export interface ImportLogEntry {
  id: number
  filename: string
  import_type: string
  total_rows: number
  imported_rows: number
  skipped_rows: number
  error_count: number
  imported_by: number
  imported_at: string
}

export interface ImportLogListResponse {
  items: ImportLogEntry[]
  total: number
  page: number
  per_page: number
}

// Coding
export interface CodingUpdatePayload {
  gutachten_typ: string | null
  therapieaenderung: string | null
  ta_diagnosekorrektur: boolean
  ta_unterversorgung: boolean
  ta_uebertherapie: boolean
}

// Notifications
export interface NotificationList {
  items: Notification[]
  unread_count: number
}

// Admin Users
export interface UserResponse {
  id: number
  username: string
  email: string
  first_name: string | null
  last_name: string | null
  display_name: string | null
  avatar_url: string | null
  role: 'admin' | 'dak_mitarbeiter'
  is_active: boolean
  mfa_enabled: boolean
  last_login: string | null
  created_at: string
}

export interface CreateUserPayload {
  username: string
  email: string
  password: string
  role: 'admin' | 'dak_mitarbeiter'
}

export interface UpdateUserPayload {
  username?: string
  email?: string
  role?: 'admin' | 'dak_mitarbeiter'
  is_active?: boolean
}

// Account / Profile
export interface ProfileUpdatePayload {
  first_name?: string
  last_name?: string
  display_name?: string
  username?: string
  email?: string
}

export interface ChangePasswordPayload {
  old_password: string
  new_password: string
}

export interface MFASetupResponse {
  secret: string
  qr_uri: string
}

export interface MFAVerifyPayload {
  secret: string
  code: string
}

// Admin Invitations
export interface InvitationResponse {
  id: number
  token: string
  email: string | null
  role: 'admin' | 'dak_mitarbeiter'
  created_at: string
  expires_at: string
  used_at: string | null
  used_by: number | null
}

export interface CreateInvitationPayload {
  email?: string
  role: 'admin' | 'dak_mitarbeiter'
  expires_in_days: number
}

// Admin Audit Log
export interface AuditLogEntry {
  id: number
  user_id: number | null
  action: string
  entity_type: string | null
  entity_id: string | null
  old_values: Record<string, unknown> | null
  new_values: Record<string, unknown> | null
  ip_address: string | null
  created_at: string
}

// Disclosure Requests
export interface DisclosureRequest {
  id: number
  case_id: number
  requester_id: number
  requester_username: string | null
  fall_id: string | null
  reason: string
  status: 'pending' | 'approved' | 'rejected'
  reviewed_by: number | null
  reviewed_at: string | null
  expires_at: string | null
  created_at: string
}

export interface DisclosureRequestCreate {
  reason: string
}

export interface DisclosureCountResponse {
  pending_count: number
}
