<script setup lang="ts">
import { Search } from '@vben/icons';
import { $t } from '@vben/locales';

import DbNodeConnectionBadge from '../components/DbNodeConnectionBadge.vue';
import BaseNode from './BaseNode.vue';

defineProps(['selected', 'data', 'id']);
</script>

<template>
  <BaseNode
    :selected="selected"
    :execution-status="data.executionStatus"
    :execution-result="data.executionResult"
    :label="data.label || $t('ai-platform.workflow.nodes.db_query.label')"
    :inputs="[{ id: 'target' }]"
    :outputs="[{ id: 'source' }]"
    :node-id="id"
    node-type="db_query"
  >
    <template #icon><Search class="size-5 text-blue-600" /></template>
    <template #desc>
      {{ $t('ai-platform.workflow.nodes.db_query.desc') }}
    </template>

    <div class="space-y-2">
      <DbNodeConnectionBadge :db-config="data.db_config" />

      <!-- 表名 -->
      <div class="flex items-center gap-2">
        <span
          class="rounded border border-blue-200 bg-blue-50 px-1.5 py-0.5 text-[10px] font-bold text-blue-600 dark:border-blue-700 dark:bg-blue-950 dark:text-blue-400"
        >
          SELECT
        </span>
        <span
          class="text-foreground flex-1 truncate font-mono text-xs"
          :title="data.table"
        >
          {{ data.table || $t('ai-platform.workflow.nodes.db_query.noTable') }}
        </span>
      </div>

      <!-- 返回字段 -->
      <div
        v-if="data.return_fields && data.return_fields !== '*'"
        class="bg-muted rounded px-2 py-1.5"
      >
        <div class="text-muted-foreground text-[10px]">
          {{ $t('ai-platform.workflow.nodes.db_query.returnMode') }}
          {{ data.return_fields }}
        </div>
      </div>

      <!-- 条件预览 -->
      <div
        v-if="data.where_conditions && data.where_conditions.length > 0"
        class="rounded bg-blue-50 px-2 py-1.5 dark:bg-blue-950"
      >
        <div class="text-[10px] text-blue-700 dark:text-blue-300">
          {{
            $t('ai-platform.workflow.nodes.common.conditions', {
              count: data.where_conditions.length,
            })
          }}
        </div>
      </div>

      <!-- 限制条数 -->
      <div
        v-if="data.limit && data.limit !== 100"
        class="text-muted-foreground text-[10px]"
      >
        {{
          $t('ai-platform.workflow.nodes.db_query.limit', { count: data.limit })
        }}
      </div>
    </div>
  </BaseNode>
</template>
