/**
 * VVT Baseline-Katalog — Helpers & Catalog Assembly
 */

import type { BusinessFunction } from '../vvt-types'
import type { BaselineTemplate } from './types'
import { HR_VVT_TEMPLATES, FINANCE_VVT_TEMPLATES } from './hr-finance'
import { SALES_CRM_VVT_TEMPLATES } from './sales-crm'
import { MARKETING_VVT_TEMPLATES } from './marketing'
import { SUPPORT_VVT_TEMPLATES, IT_OPERATIONS_VVT_TEMPLATES } from './support-it'
import { OTHER_VVT_TEMPLATES } from './other'

export const VVT_BASELINE_CATALOG: BaselineTemplate[] = [
  ...HR_VVT_TEMPLATES,
  ...FINANCE_VVT_TEMPLATES,
  ...SALES_CRM_VVT_TEMPLATES,
  ...MARKETING_VVT_TEMPLATES,
  ...SUPPORT_VVT_TEMPLATES,
  ...IT_OPERATIONS_VVT_TEMPLATES,
  ...OTHER_VVT_TEMPLATES,
]

export function templateToActivity(
  template: BaselineTemplate,
  vvtId: string,
): Omit<import('../vvt-types').VVTActivity, 'id'> & { id: string } {
  const now = new Date().toISOString()
  return {
    id: crypto.randomUUID(),
    vvtId,
    name: template.name,
    description: template.description,
    purposes: template.purposes,
    legalBases: template.legalBases,
    dataSubjectCategories: template.dataSubjectCategories,
    personalDataCategories: template.personalDataCategories,
    recipientCategories: template.recipientCategories,
    thirdCountryTransfers: [],
    retentionPeriod: template.retentionPeriod,
    tomDescription: template.tomDescription,
    businessFunction: template.businessFunction,
    systems: template.typicalSystems.map((s, i) => ({ systemId: `sys-${i}`, name: s })),
    deploymentModel: 'cloud',
    dataSources: [{ type: 'DATA_SUBJECT', description: 'Direkt von der betroffenen Person' }],
    dataFlows: [],
    protectionLevel: template.protectionLevel,
    dpiaRequired: template.dpiaRequired,
    structuredToms: template.structuredToms,
    status: 'DRAFT',
    responsible: '',
    owner: '',
    createdAt: now,
    updatedAt: now,
  }
}

export function getTemplatesByFunction(fn: BusinessFunction): BaselineTemplate[] {
  return VVT_BASELINE_CATALOG.filter(t => t.businessFunction === fn)
}

export function getTemplateById(templateId: string): BaselineTemplate | undefined {
  return VVT_BASELINE_CATALOG.find(t => t.templateId === templateId)
}
