import { parseJson } from '../db.js';

export function camelGroup(row) {
  return { id: row.id, name: row.name, description: row.description, createdAt: row.created_at };
}

export function camelCredential(row) {
  return {
    id: row.id,
    name: row.name,
    kind: row.kind,
    username: row.username,
    hasSecret: Boolean(row.secret_cipher),
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelHost(row) {
  return {
    id: row.id,
    name: row.name,
    fqdn: row.fqdn,
    address: row.address,
    osFamily: row.os_family,
    transport: row.transport,
    port: row.port,
    credentialId: row.credential_id,
    credentialName: row.credential_name || null,
    tags: parseJson(row.tags),
    notes: row.notes,
    visibility: row.visibility || 'shared',
    ownerUserId: row.owner_user_id || null,
    groupId: row.group_id || null,
    groupName: row.group_name || null,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelFolder(row) {
  return {
    id: row.id,
    parentId: row.parent_id,
    name: row.name,
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelScript(row) {
  return {
    id: row.id,
    folderId: row.folder_id,
    folderName: row.folder_name || null,
    name: row.name,
    description: row.description,
    content: row.content,
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    version: row.version,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelAssetFolder(row) {
  return {
    id: row.id,
    parentId: row.parent_id,
    name: row.name,
    description: row.description || '',
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    createdBy: row.created_by,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelAsset(row) {
  return {
    id: row.id,
    folderId: row.folder_id,
    folderName: row.folder_name || null,
    name: row.name,
    originalName: row.original_name,
    description: row.description || '',
    kind: row.kind || 'generic',
    mimeType: row.mime_type || 'application/octet-stream',
    sizeBytes: row.size_bytes || 0,
    checksum: row.checksum,
    packagePath: row.package_path || '',
    referenceToken: `{{asset:${row.id}}}`,
    downloadUrl: `/api/assets/${row.id}/download`,
    viewUrl: `/api/assets/${row.id}/view`,
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    createdBy: row.created_by,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelAssetLink(row) {
  return {
    id: row.id,
    assetId: row.asset_id,
    assetName: row.asset_name || null,
    targetType: row.target_type,
    targetId: row.target_id,
    linkRole: row.link_role,
    packagePath: row.package_path || '',
    createdBy: row.created_by,
    createdAt: row.created_at
  };
}

export function camelRunPlan(row) {
  return {
    id: row.id,
    name: row.name,
    description: row.description,
    scriptId: row.script_id,
    scriptName: row.script_name,
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    parallel: Boolean(row.parallel),
    hostCount: row.host_count || 0,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelCustomVariable(row, includeValue = false) {
  return {
    id: row.id,
    name: row.name,
    syntax: `$${row.name}`,
    token: `{{${row.name}}}`,
    description: row.description || '',
    category: row.category || 'Custom',
    valueType: row.value_type || 'string',
    value: includeValue ? row.value : undefined,
    hasValue: Boolean(row.secret_cipher),
    sensitive: Boolean(row.sensitive),
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    source: 'custom',
    readOnly: false,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelPsadtProfile(row) {
  return {
    id: row.id,
    name: row.name,
    appVendor: row.app_vendor || '',
    appName: row.app_name || '',
    appVersion: row.app_version || '',
    appArch: row.app_arch || 'x64',
    appLang: row.app_lang || 'EN',
    appRevision: row.app_revision || '01',
    toolkitVersion: row.toolkit_version || 'v4',
    templateVersion: row.template_version || 'v4',
    deploymentType: row.deployment_type || 'Install',
    deployMode: row.deploy_mode || 'Interactive',
    requireAdmin: Boolean(row.require_admin),
    zeroConfig: Boolean(row.zero_config),
    suppressReboot: Boolean(row.suppress_reboot),
    closeProcesses: parseJson(row.close_processes),
    installTasks: parseJson(row.install_tasks),
    uiPlan: parseJson(row.ui_plan, {}),
    configPlan: parseJson(row.config_plan, {}),
    admxPlan: parseJson(row.admx_plan, {}),
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelIntuneDeployment(row) {
  return {
    id: row.id,
    name: row.name,
    profileId: row.profile_id || null,
    profileName: row.profile_name || null,
    scriptId: row.script_id || null,
    scriptName: row.script_name || null,
    applicationId: row.application_id || null,
    sourceFolder: row.source_folder || '',
    intunewinFile: row.intunewin_file || '',
    appType: row.app_type,
    commandStyle: row.command_style || 'v4',
    installBehavior: row.install_behavior || 'system',
    restartBehavior: row.restart_behavior || 'return-code',
    uiMode: row.ui_mode || 'native-v4',
    requirements: parseJson(row.requirements, {}),
    installCommand: row.install_command,
    uninstallCommand: row.uninstall_command,
    detectionType: row.detection_type,
    detectionRule: row.detection_rule || '',
    returnCodes: parseJson(row.return_codes),
    assignments: parseJson(row.assignments),
    relationships: parseJson(row.relationships),
    status: row.status,
    autoPromoteAfterHours: row.auto_promote_after_hours || 0,
    autoPromoteRing: row.auto_promote_ring || '',
    autoPromoteIntent: row.auto_promote_intent || 'required',
    soakStartedAt: row.soak_started_at || '',
    promotedAt: row.promoted_at || '',
    graphAppId: row.graph_app_id || '',
    graphConnectionId: row.graph_connection_id || '',
    notes: row.notes || '',
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelGraphConnection(row) {
  return {
    id: row.id,
    name: row.name,
    tenantId: row.tenant_id,
    clientId: row.client_id,
    hasClientSecret: Boolean(row.secret_cipher),
    cloud: row.cloud || 'global',
    graphBaseUrl: row.graph_base_url || 'https://graph.microsoft.com',
    authorityHost: row.authority_host || 'https://login.microsoftonline.com',
    defaultApiVersion: row.default_api_version || 'v1.0',
    enabled: Boolean(row.enabled),
    allowWrite: Boolean(row.allow_write),
    requireApproval: Boolean(row.require_approval),
    publisherIds: parseJson(row.publisher_ids, []),
    approverIds: parseJson(row.approver_ids, []),
    lastTestStatus: row.last_test_status || '',
    lastTestMessage: row.last_test_message || '',
    lastTestAt: row.last_test_at || '',
    visibility: row.visibility,
    ownerUserId: row.owner_user_id,
    groupId: row.group_id,
    groupName: row.group_name || null,
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelIntuneDeploymentStatus(row) {
  return {
    id: row.id,
    deploymentId: row.deployment_id,
    connectionId: row.connection_id,
    graphAppId: row.graph_app_id,
    scope: row.scope,
    principalId: row.principal_id || '',
    principalName: row.principal_name || '',
    deviceId: row.device_id || '',
    deviceName: row.device_name || '',
    userId: row.user_id || '',
    userPrincipalName: row.user_principal_name || '',
    installState: row.install_state || 'unknown',
    installStateDetail: row.install_state_detail || '',
    errorCode: row.error_code || '',
    errorDescription: row.error_description || '',
    lastSyncDateTime: row.last_sync_datetime || '',
    raw: parseJson(row.raw_json, {}),
    createdAt: row.created_at,
    updatedAt: row.updated_at
  };
}

export function camelIntuneDeploymentSyncRun(row) {
  return {
    id: row.id,
    deploymentId: row.deployment_id,
    connectionId: row.connection_id,
    graphAppId: row.graph_app_id,
    status: row.status,
    message: row.message || '',
    summary: parseJson(row.summary, {}),
    startedAt: row.started_at,
    endedAt: row.ended_at
  };
}

export function camelJob(row) {
  return {
    id: row.id,
    runplanId: row.runplan_id,
    runplanName: row.runplan_name || null,
    scriptId: row.script_id,
    scriptName: row.script_name || null,
    status: row.status,
    triggeredBy: row.triggered_by,
    startedAt: row.started_at,
    endedAt: row.ended_at,
    createdAt: row.created_at
  };
}

export function camelJobHost(row) {
  return {
    id: row.id,
    jobId: row.job_id,
    hostId: row.host_id,
    hostName: row.host_name,
    hostAddress: row.host_address,
    status: row.status,
    exitCode: row.exit_code,
    startedAt: row.started_at,
    endedAt: row.ended_at
  };
}

export function camelJobLog(row) {
  return {
    id: row.id,
    jobId: row.job_id,
    hostId: row.host_id,
    hostName: row.host_name,
    stream: row.stream,
    line: row.line,
    createdAt: row.created_at
  };
}
