<script setup lang="ts">
import { ref } from 'vue'
import DetailsEditDialog from './DetailsEditDialog.vue'
import ResearchViewDialog from './ResearchViewDialog.vue'

const props = defineProps<{
  category: 'indicator' | 'strategy' | 'research'
  rows: Array<{ id: string; display_name: string; description?: string }>
}>()

const dialogVisible = ref(false)
const editingName = ref('')

const viewDialogVisible = ref(false)
const viewingName = ref('')

function openEdit(name: string) {
  editingName.value = name
  dialogVisible.value = true
}

function openView(name: string) {
  viewingName.value = name
  viewDialogVisible.value = true
}

function onUpdated(_payload: { category: string; name: string; success: boolean; error?: string }) {
  // Hook for handling the details_updated response — add logic here as needed
}
</script>

<template>
  <div class="category-list">
    <div v-if="rows.length === 0" class="empty">No items</div>
    <div v-for="row in rows" :key="row.id" class="item-row">
      <span class="item-name">{{ row.display_name }}</span>
      <span class="item-desc">{{ row.description ?? '' }}</span>
      <button v-if="category === 'research'" class="view-btn" @click="openView(row.display_name)">View</button>
      <button class="edit-btn" @click="openEdit(row.display_name)">Edit</button>
    </div>
  </div>

  <ResearchViewDialog
    v-if="category === 'research'"
    v-model:visible="viewDialogVisible"
    :category="category"
    :name="viewingName"
  />

  <DetailsEditDialog
    v-model:visible="dialogVisible"
    :category="category"
    :name="editingName"
    @updated="onUpdated"
  />
</template>

<style scoped>
.category-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.empty {
  color: #555;
  text-align: center;
  padding: 16px;
  font-size: 12px;
}

.item-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 10px;
  border-bottom: 1px solid #1e1e1e;
  min-width: 0;
}

.item-row:hover {
  background: #1a1a1a;
}

.item-name {
  font-size: 12px;
  font-weight: 600;
  color: #dbdbdb;
  white-space: nowrap;
  flex-shrink: 0;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-desc {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: #777;
  white-space: nowrap;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(to right, black calc(100% - 48px), transparent 100%);
  mask-image: linear-gradient(to right, black calc(100% - 48px), transparent 100%);
}

.view-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid #3d3d3d;
  color: #888;
  cursor: pointer;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 3px;
  line-height: 18px;
}

.view-btn:hover {
  border-color: #4a9eca;
  color: #4a9eca;
}

.edit-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid #3d3d3d;
  color: #888;
  cursor: pointer;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 3px;
  line-height: 18px;
}

.edit-btn:hover {
  border-color: #089981;
  color: #089981;
}
</style>
