import { LoeschfristPolicy, createEmptyPolicy } from '@/lib/sdk/loeschfristen-types'

export const LOESCHFRISTEN_API = '/api/sdk/v1/compliance/loeschfristen'

export function apiToPolicy(raw: any): LoeschfristPolicy {
  const base = createEmptyPolicy()
  return {
    ...base,
    id: raw.id,
    policyId: raw.policy_id || base.policyId,
    dataObjectName: raw.data_object_name || '',
    description: raw.description || '',
    affectedGroups: raw.affected_groups || [],
    dataCategories: raw.data_categories || [],
    primaryPurpose: raw.primary_purpose || '',
    deletionTrigger: raw.deletion_trigger || 'PURPOSE_END',
    retentionDriver: raw.retention_driver || null,
    retentionDriverDetail: raw.retention_driver_detail || '',
    retentionDuration: raw.retention_duration ?? null,
    retentionUnit: raw.retention_unit || null,
    retentionDescription: raw.retention_description || '',
    startEvent: raw.start_event || '',
    hasActiveLegalHold: raw.has_active_legal_hold || false,
    legalHolds: raw.legal_holds || [],
    storageLocations: raw.storage_locations || [],
    deletionMethod: raw.deletion_method || 'MANUAL_REVIEW_DELETE',
    deletionMethodDetail: raw.deletion_method_detail || '',
    responsibleRole: raw.responsible_role || '',
    responsiblePerson: raw.responsible_person || '',
    releaseProcess: raw.release_process || '',
    linkedVVTActivityIds: raw.linked_vvt_activity_ids || [],
    linkedVendorIds: raw.linked_vendor_ids || [],
    status: raw.status || 'DRAFT',
    lastReviewDate: raw.last_review_date || base.lastReviewDate,
    nextReviewDate: raw.next_review_date || base.nextReviewDate,
    reviewInterval: raw.review_interval || 'ANNUAL',
    tags: raw.tags || [],
    createdAt: raw.created_at || base.createdAt,
    updatedAt: raw.updated_at || base.updatedAt,
  }
}

export function policyToPayload(p: LoeschfristPolicy): any {
  return {
    policy_id: p.policyId,
    data_object_name: p.dataObjectName,
    description: p.description,
    affected_groups: p.affectedGroups,
    data_categories: p.dataCategories,
    primary_purpose: p.primaryPurpose,
    deletion_trigger: p.deletionTrigger,
    retention_driver: p.retentionDriver || null,
    retention_driver_detail: p.retentionDriverDetail,
    retention_duration: p.retentionDuration || null,
    retention_unit: p.retentionUnit || null,
    retention_description: p.retentionDescription,
    start_event: p.startEvent,
    has_active_legal_hold: p.hasActiveLegalHold,
    legal_holds: p.legalHolds,
    storage_locations: p.storageLocations,
    deletion_method: p.deletionMethod,
    deletion_method_detail: p.deletionMethodDetail,
    responsible_role: p.responsibleRole,
    responsible_person: p.responsiblePerson,
    release_process: p.releaseProcess,
    linked_vvt_activity_ids: p.linkedVVTActivityIds,
    linked_vendor_ids: p.linkedVendorIds,
    status: p.status,
    last_review_date: p.lastReviewDate || null,
    next_review_date: p.nextReviewDate || null,
    review_interval: p.reviewInterval,
    tags: p.tags,
  }
}
