/**
 * VVT (Verarbeitungsverzeichnis) Types — Art. 30 DSGVO
 *
 * Re-exports common types from vendor-compliance/types.ts and adds
 * VVT-specific interfaces for the 4-tab VVT module.
 */

// Re-exports from vendor-compliance/types.ts
export type {
  DataSubjectCategory,
  PersonalDataCategory,
  LegalBasisType,
  TransferMechanismType,
  RecipientCategoryType,
  ProcessingActivityStatus,
  ProtectionLevel,
  ThirdCountryTransfer,
  RetentionPeriod,
  LegalBasis,
  RecipientCategory,
  DataSource,
  SystemReference,
  DataFlow,
  DataSourceType,
  LocalizedText,
} from './vendor-compliance/types'

export {
  DATA_SUBJECT_CATEGORY_META,
  PERSONAL_DATA_CATEGORY_META,
  LEGAL_BASIS_META,
  TRANSFER_MECHANISM_META,
  isSpecialCategory,
  hasAdequacyDecision,
  generateVVTId,
} from './vendor-compliance/types'

// =============================================================================
// VVT-SPECIFIC TYPES
// =============================================================================

export interface VVTOrganizationHeader {
  organizationName: string
  industry: string
  locations: string[]
  employeeCount: number
  dpoName: string
  dpoContact: string
  vvtVersion: string
  lastReviewDate: string
  nextReviewDate: string
  reviewInterval: 'quarterly' | 'semi_annual' | 'annual'
}

export type BusinessFunction =
  | 'hr'
  | 'finance'
  | 'sales_crm'
  | 'marketing'
  | 'support'
  | 'it_operations'
  | 'product_engineering'
  | 'legal'
  | 'management'
  | 'other'

export interface StructuredTOMs {
  accessControl: string[]
  confidentiality: string[]
  integrity: string[]
  availability: string[]
  separation: string[]
}

export interface VVTActivity {
  // Pflichtfelder Art. 30 Abs. 1 (Controller)
  id: string
  vvtId: string
  name: string
  description: string
  purposes: string[]
  legalBases: { type: string; description?: string; reference?: string }[]
  dataSubjectCategories: string[]
  personalDataCategories: string[]
  recipientCategories: { type: string; name: string; description?: string; isThirdCountry?: boolean; country?: string }[]
  thirdCountryTransfers: { country: string; recipient: string; transferMechanism: string; additionalMeasures?: string[] }[]
  retentionPeriod: { duration?: number; durationUnit?: string; description: string; legalBasis?: string; deletionProcedure?: string }
  tomDescription: string

  // Generator-Optimierung (Layer B)
  businessFunction: BusinessFunction
  systems: { systemId: string; name: string; description?: string; type?: string }[]
  deploymentModel: 'cloud' | 'on_prem' | 'hybrid'
  dataSources: { type: string; description?: string }[]
  dataFlows: { sourceSystem?: string; targetSystem?: string; description: string; dataCategories: string[] }[]
  protectionLevel: 'LOW' | 'MEDIUM' | 'HIGH'
  dpiaRequired: boolean
  structuredToms: StructuredTOMs

  // Workflow
  status: 'DRAFT' | 'REVIEW' | 'APPROVED' | 'ARCHIVED'
  responsible: string
  owner: string
  createdAt: string
  updatedAt: string

  // Library refs (optional, parallel to freetext)
  purposeRefs?: string[]
  legalBasisRefs?: string[]
  dataSubjectRefs?: string[]
  dataCategoryRefs?: string[]
  recipientRefs?: string[]
  retentionRuleRef?: string
  transferMechanismRefs?: string[]
  tomRefs?: string[]
  linkedLoeschfristenIds?: string[]
  linkedTomMeasureIds?: string[]
  sourceTemplateId?: string
  riskScore?: number
  art30Completeness?: VVTCompleteness
}

// =============================================================================
// CONSTANTS
// =============================================================================

export const BUSINESS_FUNCTION_LABELS: Record<BusinessFunction, string> = {
  hr: 'Personal (HR)',
  finance: 'Finanzen & Buchhaltung',
  sales_crm: 'Vertrieb & CRM',
  marketing: 'Marketing',
  support: 'Kundenservice',
  it_operations: 'IT-Betrieb',
  product_engineering: 'Produktentwicklung',
  legal: 'Recht & Compliance',
  management: 'Geschaeftsfuehrung',
  other: 'Sonstiges',
}

export const STATUS_LABELS: Record<string, string> = {
  DRAFT: 'Entwurf',
  REVIEW: 'In Pruefung',
  APPROVED: 'Genehmigt',
  ARCHIVED: 'Archiviert',
}

export const STATUS_COLORS: Record<string, string> = {
  DRAFT: 'bg-gray-100 text-gray-700',
  REVIEW: 'bg-yellow-100 text-yellow-700',
  APPROVED: 'bg-green-100 text-green-700',
  ARCHIVED: 'bg-red-100 text-red-700',
}

export const PROTECTION_LEVEL_LABELS: Record<string, string> = {
  LOW: 'Niedrig',
  MEDIUM: 'Mittel',
  HIGH: 'Hoch',
}

export const DEPLOYMENT_LABELS: Record<string, string> = {
  cloud: 'Cloud',
  on_prem: 'On-Premise',
  hybrid: 'Hybrid',
}

export const REVIEW_INTERVAL_LABELS: Record<string, string> = {
  quarterly: 'Vierteljaehrlich',
  semi_annual: 'Halbjaehrlich',
  annual: 'Jaehrlich',
}

// Art. 9 special categories for highlighting
export const ART9_CATEGORIES: string[] = [
  'HEALTH_DATA',
  'GENETIC_DATA',
  'BIOMETRIC_DATA',
  'RACIAL_ETHNIC',
  'POLITICAL_OPINIONS',
  'RELIGIOUS_BELIEFS',
  'TRADE_UNION',
  'SEX_LIFE',
  'CRIMINAL_DATA',
]

export interface VVTCompleteness {
  score: number
  missing: string[]
  warnings: string[]
  passed: number
  total: number
}

// =============================================================================
// HELPER: Create empty activity
// =============================================================================

export function createEmptyActivity(vvtId: string): VVTActivity {
  const now = new Date().toISOString()
  return {
    id: crypto.randomUUID(),
    vvtId,
    name: '',
    description: '',
    purposes: [],
    legalBases: [],
    dataSubjectCategories: [],
    personalDataCategories: [],
    recipientCategories: [],
    thirdCountryTransfers: [],
    retentionPeriod: { description: '', legalBasis: '', deletionProcedure: '' },
    tomDescription: '',
    businessFunction: 'other',
    systems: [],
    deploymentModel: 'cloud',
    dataSources: [],
    dataFlows: [],
    protectionLevel: 'MEDIUM',
    dpiaRequired: false,
    structuredToms: {
      accessControl: [],
      confidentiality: [],
      integrity: [],
      availability: [],
      separation: [],
    },
    status: 'DRAFT',
    responsible: '',
    owner: '',
    createdAt: now,
    updatedAt: now,
  }
}

// =============================================================================
// HELPER: Default organization header
// =============================================================================

export function createDefaultOrgHeader(): VVTOrganizationHeader {
  return {
    organizationName: '',
    industry: '',
    locations: [],
    employeeCount: 0,
    dpoName: '',
    dpoContact: '',
    vvtVersion: '1.0',
    lastReviewDate: new Date().toISOString().split('T')[0],
    nextReviewDate: '',
    reviewInterval: 'annual',
  }
}
