<script setup lang="ts">
interface ModelOption {
  id: number;
  name: string;
  modelId: string;
  providerName: string;
}

const props = defineProps<{
  modelId: number | null;
  models: ModelOption[];
  loggedIn: boolean;
  rateLimitInfo?: {
    sessionRemaining: number | null;
    ipRemaining: number | null;
  };
}>();

const emit = defineEmits<{
  "update:modelId": [modelId: number | null];
  showPrompt: [];
  toggleSidebar: [];
}>();
</script>

<template>
  <div class="agent-toolbar">
    <div class="toolbar-left">
      <button class="icon-btn" title="菜单" @click="emit('toggleSidebar')">
        <Icon name="lucide:menu" />
      </button>
      <AgentModelSelector
        :model-id="modelId"
        :models="models"
        @update="emit('update:modelId', $event)"
      />
    </div>

    <div class="toolbar-right">
      <!-- Rate limit indicator (guest only) -->
      <div v-if="!loggedIn && rateLimitInfo" class="rate-limit-info" :data-short="rateLimitInfo.sessionRemaining !== null ? `${rateLimitInfo.sessionRemaining}/20` : ''">
        <Icon name="lucide:zap" class="rl-icon" />
        <span v-if="rateLimitInfo.sessionRemaining !== null">
          本对话 {{ rateLimitInfo.sessionRemaining }}/20
        </span>
        <span v-if="rateLimitInfo.ipRemaining !== null" class="rl-sep">·</span>
        <span v-if="rateLimitInfo.ipRemaining !== null">
          今日 {{ rateLimitInfo.ipRemaining }}/50
        </span>
      </div>

      <!-- System prompt button -->
      <button class="icon-btn" title="系统提示词" @click="emit('showPrompt')">
        <Icon name="lucide:info" />
      </button>
    </div>
  </div>
</template>

<style scoped>
.agent-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 24px;
  border-bottom: 1px solid var(--color-hairline);
  background: var(--color-canvas);
  flex-shrink: 0;
}

.toolbar-left,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  background: none;
  border-radius: 8px;
  cursor: pointer;
  color: var(--color-muted);
  font-size: 18px;
  transition: all 0.15s;
}

.icon-btn:hover {
  background: var(--color-surface-soft);
  color: var(--color-ink);
}

.toggle-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: var(--color-surface-soft);
  border: 1px solid var(--color-hairline);
  border-radius: 8px;
  font-size: 13px;
  color: var(--color-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.toggle-btn:hover {
  border-color: var(--color-primary);
}

.toggle-btn.active {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-on-primary);
}

.rate-limit-info {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  background: var(--color-surface-soft);
  border: 1px solid var(--color-hairline);
  border-radius: 8px;
  font-size: 12px;
  color: var(--color-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.rl-icon {
  font-size: 13px;
  color: var(--color-accent-amber);
  flex-shrink: 0;
}

.rl-sep {
  margin: 0 2px;
}

/* ── Mobile ── */

@media (max-width: 767.98px) {
  .agent-toolbar {
    padding: 10px 12px;
  }

  .toolbar-left,
  .toolbar-right {
    gap: 6px;
    min-width: 0;
  }

  .toolbar-left {
    flex: 1;
    min-width: 0;
  }

  .rate-limit-info {
    padding: 4px 8px;
    font-size: 11px;
  }

  .rate-limit-info .rl-sep,
  .rate-limit-info > span:not(.rl-icon) {
    display: none;
  }

  .rate-limit-info::after {
    content: attr(data-short);
    font-size: 11px;
  }
}
</style>
