<script setup>
import { useNamespace, useLocale } from "@yh-ui/hooks";
import { computed, ref } from "vue";
import { aiAgentCardProps, aiAgentCardEmits } from "./ai-agent-card";
import { YhIcon } from "../../icon";
import { YhTooltip } from "../../tooltip";
import { useComponentTheme } from "@yh-ui/theme";
defineOptions({
  name: "YhAiAgentCard"
});
const props = defineProps(aiAgentCardProps);
const emit = defineEmits(aiAgentCardEmits);
const ns = useNamespace("ai-agent-card");
const { t } = useLocale();
const { themeStyle } = useComponentTheme("ai-agent-card", props.themeOverrides);
const localFavorited = ref(props.data?.favorited ?? false);
const statusClass = computed(() => {
  const s = props.data?.status;
  if (s === "online") return "is-online";
  if (s === "busy") return "is-busy";
  return "is-offline";
});
const ratingStars = computed(() => {
  const r = props.data?.stats?.rating ?? 0;
  return Array.from({ length: 5 }, (_, i) => i < Math.floor(r));
});
const formattedUses = computed(() => {
  const u = props.data?.stats?.uses ?? 0;
  if (u >= 1e4) return `${(u / 1e4).toFixed(1)}w`;
  if (u >= 1e3) return `${(u / 1e3).toFixed(1)}k`;
  return String(u);
});
const handleClick = () => {
  if (!props.loading) emit("click", props.data);
};
const handleUse = (e) => {
  e.stopPropagation();
  emit("use", props.data);
};
const handleFavorite = (e) => {
  e.stopPropagation();
  localFavorited.value = !localFavorited.value;
  emit("favorite", props.data, localFavorited.value);
};
const handleShare = (e) => {
  e.stopPropagation();
  emit("share", props.data);
};
</script>

<template>
  <!-- Skeleton 加载态 -->
  <div v-if="loading" :class="[ns.b(), ns.m(layout), ns.is('loading', true)]" :style="themeStyle">
    <div :class="ns.e('skeleton-avatar')"></div>
    <div :class="ns.e('skeleton-lines')">
      <div :class="ns.e('skeleton-line')"></div>
      <div :class="ns.e('skeleton-line')" style="width: 70%"></div>
      <div :class="ns.e('skeleton-line')" style="width: 50%"></div>
    </div>
  </div>

  <!-- 正常卡片 -->
  <div
    v-else
    :class="[ns.b(), ns.m(layout), ns.is('selected', selected)]"
    :style="themeStyle"
    @click="handleClick"
  >
    <!-- 收藏 & 分享操作（右上角浮层） -->
    <div :class="ns.e('overlay-actions')">
      <YhTooltip
        v-if="favoritable"
        :content="localFavorited ? t('ai.agent.unfavorite') : t('ai.agent.favorite')"
        placement="top"
      >
        <button :class="[ns.e('fav-btn'), ns.is('active', localFavorited)]" @click="handleFavorite">
          <YhIcon :name="localFavorited ? 'star-filled' : 'star'" />
        </button>
      </YhTooltip>

      <YhTooltip :content="t('ai.agent.share')" placement="top">
        <button :class="ns.e('share-btn')" @click="handleShare">
          <YhIcon name="share" />
        </button>
      </YhTooltip>
    </div>

    <!-- 头像区域 -->
    <div :class="ns.e('avatar-wrapper')">
      <div :class="ns.e('avatar')">
        <slot name="avatar">
          <img v-if="data.avatar?.startsWith('http')" :src="data.avatar" :alt="data.name" />
          <YhIcon v-else :name="data.avatar || 'robot'" />
        </slot>
      </div>
      <!-- 状态指示器 -->
      <span v-if="data.status" :class="[ns.e('status-dot'), statusClass]"></span>
    </div>

    <!-- 内容主区域 -->
    <div :class="ns.e('body')">
      <div :class="ns.e('header')">
        <div :class="ns.e('name-row')">
          <span :class="ns.e('name')">{{ data.name }}</span>
          <YhIcon v-if="data.verified" name="checkmark-circle" :class="ns.e('verified-icon')" />
        </div>
        <span v-if="data.model" :class="ns.e('model')">{{ data.model }}</span>
      </div>

      <p v-if="data.description" :class="ns.e('description')">{{ data.description }}</p>

      <!-- 能力标签 -->
      <div v-if="data.capabilities && data.capabilities.length > 0" :class="ns.e('capabilities')">
        <span
          v-for="(cap, idx) in data.capabilities.slice(0, 4)"
          :key="idx"
          :class="[ns.e('capability-tag'), cap.type ? ns.m(cap.type) : '']"
        >
          <YhIcon v-if="cap.icon" :name="cap.icon" />
          {{ cap.label }}
        </span>
        <span v-if="data.capabilities.length > 4" :class="ns.e('capability-more')">
          +{{ data.capabilities.length - 4 }}
        </span>
      </div>

      <!-- 统计信息 -->
      <div v-if="showStats && data.stats" :class="ns.e('stats')">
        <span v-if="data.stats.uses !== void 0" :class="ns.e('stat-item')">
          <YhIcon name="chart-bar" />
          {{ formattedUses }} {{ t("ai.agent.uses") }}
        </span>
        <span v-if="data.stats.rating !== void 0" :class="ns.e('stat-item')">
          <span :class="ns.e('stars')">
            <YhIcon
              v-for="(full, i) in ratingStars"
              :key="i"
              :name="full ? 'star-filled' : 'star'"
              :class="ns.is('filled', full)"
            />
          </span>
          <span>{{ data.stats.rating.toFixed(1) }}</span>
          <span v-if="data.stats.reviewCount" :class="ns.e('review-count')"
            >({{ data.stats.reviewCount }})</span
          >
        </span>
        <span v-if="data.stats.responseTime !== void 0" :class="ns.e('stat-item')">
          <YhIcon name="time" />
          {{ data.stats.responseTime }}ms
        </span>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div v-if="showActions" :class="ns.e('actions')">
      <slot name="actions" :data="data" :use="handleUse">
        <button :class="ns.e('use-btn')" @click="handleUse">
          <YhIcon name="sparkles" />
          {{ t("ai.agent.use") }}
        </button>
      </slot>
    </div>

    <!-- 默认插槽：额外内容 -->
    <slot :data="data"></slot>
  </div>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-ai-agent-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 20px;
  background: var(--yh-bg-color);
  border: 1.5px solid var(--yh-border-color-light);
  border-radius: var(--yh-border-radius-large, 12px);
  cursor: pointer;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}
.yh-ai-agent-card::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, var(--yh-color-primary-light-9, rgba(64, 158, 255, 0.04)) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.25s;
  pointer-events: none;
}
.yh-ai-agent-card:hover {
  border-color: var(--yh-color-primary-light-5, #79bbff);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1), 0 0 0 1px var(--yh-color-primary-light-6, #a0cfff);
  transform: translateY(-2px);
}
.yh-ai-agent-card:hover::before {
  opacity: 1;
}
.yh-ai-agent-card:hover .yh-ai-agent-card__overlay-actions {
  opacity: 1;
}

.yh-ai-agent-card:hover .yh-ai-agent-card__use-btn {
  background: var(--yh-color-primary);
  color: #fff;
}

.yh-ai-agent-card.is-selected {
  border-color: var(--yh-color-primary);
  box-shadow: 0 0 0 2px var(--yh-color-primary-light-6, #a0cfff);
}
.yh-ai-agent-card.is-selected::before {
  opacity: 1;
}
.yh-ai-agent-card--horizontal {
  flex-direction: row;
  align-items: flex-start;
}
.yh-ai-agent-card--horizontal .yh-ai-agent-card__avatar-wrapper {
  flex-shrink: 0;
}

.yh-ai-agent-card--horizontal .yh-ai-agent-card__body {
  flex: 1;
  min-width: 0;
}

.yh-ai-agent-card--horizontal .yh-ai-agent-card__actions {
  flex-shrink: 0;
  flex-direction: column;
  align-self: center;
  padding-right: 32px;
}

.yh-ai-agent-card--horizontal .yh-ai-agent-card__overlay-actions {
  flex-direction: column;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
}
.yh-ai-agent-card--horizontal .yh-ai-agent-card__overlay-actions button {
  width: 24px;
  height: 24px;
  font-size: 13px;
  background: var(--yh-bg-color);
  box-shadow: var(--yh-box-shadow-light);
}

.yh-ai-agent-card--compact {
  padding: 12px 16px;
  flex-direction: row;
  align-items: center;
  gap: 12px;
}
.yh-ai-agent-card--compact .yh-ai-agent-card__description {
  display: none;
}

.yh-ai-agent-card--compact .yh-ai-agent-card__stats {
  display: flex;
  margin-top: 0;
  gap: 12px;
}
.yh-ai-agent-card--compact .yh-ai-agent-card__stats .yh-icon {
  font-size: 13px;
}

.yh-ai-agent-card--compact .yh-ai-agent-card__capabilities {
  display: none;
}

.yh-ai-agent-card--compact .yh-ai-agent-card__actions {
  margin-top: 0;
  flex-shrink: 0;
  padding-right: 32px;
}

.yh-ai-agent-card--compact .yh-ai-agent-card__overlay-actions {
  flex-direction: column;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
}
.yh-ai-agent-card--compact .yh-ai-agent-card__overlay-actions button {
  width: 24px;
  height: 24px;
  border-radius: 5px;
  font-size: 12px;
  background: var(--yh-bg-color);
  box-shadow: var(--yh-box-shadow-light);
}

.yh-ai-agent-card__overlay-actions {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
  z-index: 1;
}
.yh-ai-agent-card__overlay-actions button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: var(--yh-bg-color-overlay, rgba(255, 255, 255, 0.85));
  backdrop-filter: blur(8px);
  color: var(--yh-text-color-regular);
  cursor: pointer;
  transition: all 0.15s;
  font-size: 14px;
}
.yh-ai-agent-card__overlay-actions button:hover {
  background: var(--yh-color-primary-light-9);
  color: var(--yh-color-primary);
}

.yh-ai-agent-card__fav-btn.is-active {
  color: var(--yh-color-warning) !important;
}

.yh-ai-agent-card__avatar-wrapper {
  position: relative;
  width: fit-content;
}

.yh-ai-agent-card__avatar {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--yh-color-primary-light-8), var(--yh-color-primary-light-5));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: var(--yh-color-primary);
  overflow: hidden;
}
.yh-ai-agent-card__avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 14px;
}

.yh-ai-agent-card__status-dot {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--yh-bg-color);
}
.yh-ai-agent-card__status-dot.is-online {
  background: var(--yh-color-success);
}
.yh-ai-agent-card__status-dot.is-busy {
  background: var(--yh-color-warning);
}
.yh-ai-agent-card__status-dot.is-offline {
  background: var(--yh-text-color-disabled);
}

.yh-ai-agent-card__body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.yh-ai-agent-card__header {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.yh-ai-agent-card__name-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.yh-ai-agent-card__name {
  font-size: 16px;
  font-weight: 600;
  color: var(--yh-text-color-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.yh-ai-agent-card__verified-icon {
  color: var(--yh-color-primary);
  font-size: 16px;
  flex-shrink: 0;
}

.yh-ai-agent-card__model {
  font-size: 12px;
  color: var(--yh-text-color-secondary);
}

.yh-ai-agent-card__description {
  font-size: 13px;
  color: var(--yh-text-color-regular);
  line-height: 1.6;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.yh-ai-agent-card__capabilities {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.yh-ai-agent-card__capability-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 500;
  background: var(--yh-color-primary-light-9);
  color: var(--yh-color-primary);
  border: 1px solid var(--yh-color-primary-light-6);
}
.yh-ai-agent-card__capability-tag--primary {
  background: var(--yh-color-primary-light-9);
  color: var(--yh-color-primary);
  border-color: var(--yh-color-primary-light-6);
}

.yh-ai-agent-card__capability-tag--success {
  background: var(--yh-color-success-light-9);
  color: var(--yh-color-success);
  border-color: var(--yh-color-success-light-5);
}

.yh-ai-agent-card__capability-tag--warning {
  background: var(--yh-color-warning-light-9);
  color: var(--yh-color-warning);
  border-color: var(--yh-color-warning-light-5);
}

.yh-ai-agent-card__capability-tag--danger {
  background: var(--yh-color-danger-light-9);
  color: var(--yh-color-danger);
  border-color: var(--yh-color-danger-light-5);
}

.yh-ai-agent-card__capability-tag--info {
  background: var(--yh-color-info-light-9);
  color: var(--yh-color-info);
  border-color: var(--yh-color-info-light-5);
}

.yh-ai-agent-card__capability-more {
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 11px;
  background: var(--yh-fill-color-light);
  color: var(--yh-text-color-secondary);
}

.yh-ai-agent-card__stats {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 2px;
}

.yh-ai-agent-card__stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--yh-text-color-secondary);
}
.yh-ai-agent-card__stat-item .yh-icon {
  font-size: 13px;
}

.yh-ai-agent-card__stars {
  display: flex;
  gap: 1px;
}
.yh-ai-agent-card__stars .yh-icon {
  font-size: 12px;
  color: var(--yh-text-color-disabled);
}
.yh-ai-agent-card__stars .yh-icon.is-filled {
  color: var(--yh-color-warning);
}

.yh-ai-agent-card__review-count {
  color: var(--yh-text-color-disabled);
  font-size: 11px;
}

.yh-ai-agent-card__actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.yh-ai-agent-card__use-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 8px 16px;
  border: 1.5px solid var(--yh-color-primary-light-5, #79bbff);
  border-radius: var(--yh-border-radius-base, 6px);
  background: transparent;
  color: var(--yh-color-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}
.yh-ai-agent-card__use-btn:hover {
  background: var(--yh-color-primary);
  color: #fff;
  border-color: var(--yh-color-primary);
}

.yh-ai-agent-card.is-loading {
  cursor: default;
  pointer-events: none;
}
.yh-ai-agent-card__skeleton-avatar {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: linear-gradient(90deg, var(--yh-fill-color) 25%, var(--yh-fill-color-light) 50%, var(--yh-fill-color) 75%);
  background-size: 200% 100%;
  animation: yh-agent-shimmer 1.5s infinite;
}

.yh-ai-agent-card__skeleton-lines {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 4px;
}

.yh-ai-agent-card__skeleton-line {
  height: 14px;
  border-radius: 7px;
  width: 100%;
  background: linear-gradient(90deg, var(--yh-fill-color) 25%, var(--yh-fill-color-light) 50%, var(--yh-fill-color) 75%);
  background-size: 200% 100%;
  animation: yh-agent-shimmer 1.5s infinite;
}

@keyframes yh-agent-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}
</style>
