/**
 * DSR (Data Subject Request) Types — Core Types & Constants
 *
 * Enums, constants, metadata, and main interfaces for GDPR Art. 15-21
 */

// =============================================================================
// ENUMS & CONSTANTS
// =============================================================================

export type DSRType =
  | 'access'       // Art. 15
  | 'rectification' // Art. 16
  | 'erasure'      // Art. 17
  | 'restriction'  // Art. 18
  | 'portability'  // Art. 20
  | 'objection'    // Art. 21

export type DSRStatus =
  | 'intake'
  | 'identity_verification'
  | 'processing'
  | 'completed'
  | 'rejected'
  | 'cancelled'

export type DSRPriority = 'low' | 'normal' | 'high' | 'critical'

export type DSRSource =
  | 'web_form' | 'email' | 'letter' | 'phone' | 'in_person' | 'other'

export type IdentityVerificationMethod =
  | 'id_document' | 'email' | 'phone' | 'postal' | 'existing_account' | 'other'

export type CommunicationType = 'incoming' | 'outgoing' | 'internal'

export type CommunicationChannel =
  | 'email' | 'letter' | 'phone' | 'portal' | 'internal_note'

// =============================================================================
// DSR TYPE METADATA
// =============================================================================

export interface DSRTypeInfo {
  type: DSRType
  article: string
  label: string
  labelShort: string
  description: string
  defaultDeadlineDays: number
  maxExtensionMonths: number
  color: string
  bgColor: string
  processDocument?: string
  /** Document type in the legal_templates table for the Document Generator */
  templateType?: string
}

export const DSR_TYPE_INFO: Record<DSRType, DSRTypeInfo> = {
  access: {
    type: 'access', article: 'Art. 15', label: 'Auskunftsrecht', labelShort: 'Auskunft',
    description: 'Recht auf Auskunft ueber gespeicherte personenbezogene Daten',
    defaultDeadlineDays: 30, maxExtensionMonths: 2,
    color: 'text-blue-700', bgColor: 'bg-blue-100',
    processDocument: 'Prozessbeschreibung Art. 15 DSGVO_v02.pdf',
    templateType: 'dsr_process_art15',
  },
  rectification: {
    type: 'rectification', article: 'Art. 16', label: 'Berichtigungsrecht', labelShort: 'Berichtigung',
    description: 'Recht auf Berichtigung unrichtiger personenbezogener Daten',
    defaultDeadlineDays: 14, maxExtensionMonths: 2,
    color: 'text-yellow-700', bgColor: 'bg-yellow-100',
    processDocument: 'Prozessbeschreibung Art. 16 DSGVO_v02.pdf',
    templateType: 'dsr_process_art16',
  },
  erasure: {
    type: 'erasure', article: 'Art. 17', label: 'Loeschungsrecht', labelShort: 'Loeschung',
    description: 'Recht auf Loeschung personenbezogener Daten ("Recht auf Vergessenwerden")',
    defaultDeadlineDays: 14, maxExtensionMonths: 2,
    color: 'text-red-700', bgColor: 'bg-red-100',
    processDocument: 'Prozessbeschreibung Art. 17 DSGVO_v03.pdf',
    templateType: 'dsr_process_art17',
  },
  restriction: {
    type: 'restriction', article: 'Art. 18', label: 'Einschraenkungsrecht', labelShort: 'Einschraenkung',
    description: 'Recht auf Einschraenkung der Verarbeitung',
    defaultDeadlineDays: 14, maxExtensionMonths: 2,
    color: 'text-orange-700', bgColor: 'bg-orange-100',
    processDocument: 'Prozessbeschreibung Art. 18 DSGVO_v01.pdf',
    templateType: 'dsr_process_art18',
  },
  portability: {
    type: 'portability', article: 'Art. 20', label: 'Datenuebertragbarkeit', labelShort: 'Uebertragung',
    description: 'Recht auf Datenuebertragbarkeit in maschinenlesbarem Format',
    defaultDeadlineDays: 30, maxExtensionMonths: 2,
    color: 'text-purple-700', bgColor: 'bg-purple-100',
    processDocument: 'Prozessbeschreibung Art. 20 DSGVO_v02.pdf',
    templateType: 'dsr_process_art20',
  },
  objection: {
    type: 'objection', article: 'Art. 21', label: 'Widerspruchsrecht', labelShort: 'Widerspruch',
    description: 'Recht auf Widerspruch gegen die Verarbeitung',
    defaultDeadlineDays: 30, maxExtensionMonths: 0,
    color: 'text-gray-700', bgColor: 'bg-gray-100',
    templateType: 'dsr_process_art21',
  }
}

export const DSR_STATUS_INFO: Record<DSRStatus, { label: string; color: string; bgColor: string; borderColor: string }> = {
  intake: { label: 'Eingang', color: 'text-blue-700', bgColor: 'bg-blue-100', borderColor: 'border-blue-200' },
  identity_verification: { label: 'ID-Pruefung', color: 'text-yellow-700', bgColor: 'bg-yellow-100', borderColor: 'border-yellow-200' },
  processing: { label: 'In Bearbeitung', color: 'text-purple-700', bgColor: 'bg-purple-100', borderColor: 'border-purple-200' },
  completed: { label: 'Abgeschlossen', color: 'text-green-700', bgColor: 'bg-green-100', borderColor: 'border-green-200' },
  rejected: { label: 'Abgelehnt', color: 'text-red-700', bgColor: 'bg-red-100', borderColor: 'border-red-200' },
  cancelled: { label: 'Storniert', color: 'text-gray-700', bgColor: 'bg-gray-100', borderColor: 'border-gray-200' },
}

// =============================================================================
// MAIN INTERFACES
// =============================================================================

export interface DSRRequester {
  name: string
  email: string
  phone?: string
  address?: string
  customerId?: string
}

export interface DSRIdentityVerification {
  verified: boolean
  method?: IdentityVerificationMethod
  verifiedAt?: string
  verifiedBy?: string
  notes?: string
  documentRef?: string
}

export interface DSRAssignment {
  assignedTo: string | null
  assignedAt?: string
  assignedBy?: string
}

export interface DSRDeadline {
  originalDeadline: string
  currentDeadline: string
  extended: boolean
  extensionReason?: string
  extensionApprovedBy?: string
  extensionApprovedAt?: string
}

export interface DSRRequest {
  id: string
  referenceNumber: string
  type: DSRType
  status: DSRStatus
  priority: DSRPriority
  requester: DSRRequester
  source: DSRSource
  sourceDetails?: string
  requestText?: string
  receivedAt: string
  deadline: DSRDeadline
  completedAt?: string
  identityVerification: DSRIdentityVerification
  assignment: DSRAssignment
  notes?: string
  internalNotes?: string
  erasureChecklist?: DSRErasureChecklist
  dataExport?: DSRDataExport
  rectificationDetails?: DSRRectificationDetails
  objectionDetails?: DSRObjectionDetails
  createdAt: string
  createdBy: string
  updatedAt: string
  updatedBy?: string
  tenantId: string
}

// =============================================================================
// TYPE-SPECIFIC INTERFACES
// =============================================================================

export interface DSRErasureChecklistItem {
  id: string
  article: string
  label: string
  description: string
  checked: boolean
  applies: boolean
  notes?: string
}

export interface DSRErasureChecklist {
  items: DSRErasureChecklistItem[]
  canProceedWithErasure: boolean
  reviewedBy?: string
  reviewedAt?: string
}

export const ERASURE_EXCEPTIONS: Omit<DSRErasureChecklistItem, 'checked' | 'applies' | 'notes'>[] = [
  { id: 'art17_3_a', article: '17(3)(a)', label: 'Meinungs- und Informationsfreiheit', description: 'Ausuebung des Rechts auf freie Meinungsaeusserung und Information' },
  { id: 'art17_3_b', article: '17(3)(b)', label: 'Rechtliche Verpflichtung', description: 'Erfuellung einer rechtlichen Verpflichtung (z.B. Aufbewahrungspflichten)' },
  { id: 'art17_3_c', article: '17(3)(c)', label: 'Oeffentliches Interesse', description: 'Gruende des oeffentlichen Interesses im Bereich Gesundheit' },
  { id: 'art17_3_d', article: '17(3)(d)', label: 'Archivzwecke', description: 'Archivzwecke, wissenschaftliche/historische Forschung, Statistik' },
  { id: 'art17_3_e', article: '17(3)(e)', label: 'Rechtsansprueche', description: 'Geltendmachung, Ausuebung oder Verteidigung von Rechtsanspruechen' },
]

export interface DSRDataExport {
  format: 'json' | 'csv' | 'xml' | 'pdf'
  generatedAt?: string
  generatedBy?: string
  fileUrl?: string
  fileName?: string
  fileSize?: number
  includesThirdPartyData: boolean
  anonymizedFields?: string[]
  transferMethod?: 'download' | 'email' | 'third_party'
  transferRecipient?: string
}

export interface DSRRectificationDetails {
  fieldsToCorrect: {
    field: string
    currentValue: string
    requestedValue: string
    corrected: boolean
    correctedAt?: string
    correctedBy?: string
  }[]
}

export interface DSRObjectionDetails {
  processingPurpose: string
  legalBasis: string
  objectionGrounds: string
  decision: 'accepted' | 'rejected' | 'pending'
  decisionReason?: string
  decisionBy?: string
  decisionAt?: string
}
