<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import KsListPage from '@shared/ui/components/KsListPage.vue'
import { KsButton, KsDataGrid, KsTextField } from '@shared/ui/components'
import { useKbxRegistry } from '@shared/composables/useKbxRegistry'
import { useModelsList, type Model } from '../composables/useModels'
import type { ModelListParams } from '../composables/useModels'
import { toUiGridColumns } from '@shared/ui/gridColumnAdapter'

const router = useRouter()
const registry = useKbxRegistry()

// Get screen definition from registry
const screenDef = computed(() =>
  registry.getScreen('model-ops.models.list'),
)

const modelColumns = computed(() => toUiGridColumns(screenDef.value?.grid?.columnDefs ?? []))

// Search and filter state
const searchQuery = ref('')
const phaseFilter = ref('all')
const activeFilter = ref('all')

// Pagination
const currentPage = ref(1)
const pageSize = ref(50)

// Query parameters
const queryParams = computed<ModelListParams>(() => ({
  page: currentPage.value,
  pageSize: pageSize.value,
  search: searchQuery.value || undefined,
  phase: phaseFilter.value === 'all' ? undefined : phaseFilter.value,
  active: activeFilter.value === 'active' ? true : undefined,
}))

// TanStack Query hook
const modelsQuery = useModelsList(queryParams.value)

const dataState = computed<'idle' | 'pending' | 'ready' | 'error' | 'empty'>(() => {
  if (modelsQuery.isPending.value) return 'pending'
  if (modelsQuery.isError.value) return 'error'
  if (modelsQuery.data.value?.items.length === 0) return 'empty'
  return 'ready'
})

// Quick filters
const quickFilters = computed(() => {
  const items = modelsQuery.data.value?.items || []
  return [
    { id: 'all', label: 'All', active: phaseFilter.value === 'all', badge: items.length },
    { id: 'active', label: 'Active', active: activeFilter.value === 'active', badge: items.filter(m => m.active).length },
    { id: 'ready', label: 'Ready to Deploy', active: phaseFilter.value === 'ready', badge: 2 },
  ]
})

// Summary items
const summaryItems = computed(() => {
  const items = modelsQuery.data.value?.items || []
  const avgPbo = items.length > 0 ? (items.reduce((sum, m) => sum + m.pbo, 0) / items.length).toFixed(1) : '0'

  return [
    { label: 'Total Models', value: items.length },
    { label: 'Active', value: items.filter(m => m.active).length },
    { label: 'Ready to Deploy', value: 2 },
    { label: 'Avg PBO', value: avgPbo },
  ]
})

// Actions
const handleSearch = () => {
  modelsQuery.refetch()
}

const handleNewModel = () => {
  router.push('/model-ops/models/new')
}

const handleRowClick = (modelId: string) => {
  router.push(`/model-ops/models/${modelId}`)
}

const handleRowSelected = (row: unknown) => {
  const model = row as Partial<Model>
  if (typeof model.modelId === 'string') handleRowClick(model.modelId)
}

const handleQuickFilter = (filterId: string) => {
  if (filterId === 'active') {
    activeFilter.value = activeFilter.value === 'active' ? 'all' : 'active'
  } else {
    phaseFilter.value = filterId
  }
}

const handleRefresh = () => {
  handleSearch()
}

// Keyboard shortcuts
const handleKeydown = (e: KeyboardEvent) => {
  if (e.key === 'F3') {
    e.preventDefault()
    handleSearch()
  } else if (e.ctrlKey && e.key === 'n') {
    e.preventDefault()
    handleNewModel()
  }
}

onMounted(() => {
  window.addEventListener('keydown', handleKeydown)
})

onUnmounted(() => {
  window.removeEventListener('keydown', handleKeydown)
})
</script>

<template>
  <div v-if="screenDef" class="models-list">
    <KsListPage
      :screen="screenDef"
      :data-state="dataState"
      :loading="dataState === 'pending'"
      :summary-items="summaryItems"
      :quick-filters="quickFilters"
      @quick-filter="handleQuickFilter"
      @refresh="handleRefresh"
    >
      <!-- Header Actions -->
      <template #header-actions>
        <KsButton
          label="New Model"
          severity="primary"
          @click="handleNewModel"
        />
      </template>

      <!-- Search Panel -->
      <template #search>
        <div class="models-search">
          <div class="search-row">
            <KsTextField
              v-model="searchQuery"
              label="Model search"
              placeholder="Search by model name..."
              @keydown.enter="handleSearch"
            />
            <KsButton
              label="Search"
              severity="secondary"
              @click="handleSearch"
            />
          </div>
          <div class="search-row">
            <select v-model="phaseFilter" class="phase-filter">
              <option value="all">All Phases</option>
              <option value="freeze">Freeze</option>
              <option value="mature">Mature</option>
              <option value="score">Score</option>
              <option value="diagnose">Diagnose</option>
              <option value="hypothesis">Hypothesis</option>
              <option value="challenger">Challenger</option>
              <option value="validate">Validate</option>
              <option value="review">Review</option>
            </select>
            <select v-model="activeFilter" class="active-filter">
              <option value="all">All Status</option>
              <option value="active">Active</option>
              <option value="inactive">Inactive</option>
            </select>
          </div>
        </div>
      </template>

      <!-- Content Area -->
      <template #content>
        <KsDataGrid
          v-if="screenDef.grid && modelsQuery.data.value?.items"
          :columns="modelsQuery.data.value?.items.length ? modelColumns : []"
          :rows="modelsQuery.data.value?.items || []"
          :loading="modelsQuery.isPending.value"
          @row-selected="handleRowSelected"
        />
      </template>
    </KsListPage>
  </div>
</template>

<style scoped>
.models-list {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.models-search {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  background: var(--kbx-color-surface, #f5f5f5);
  border-radius: 4px;
}

.search-row {
  display: flex;
  gap: 12px;
  align-items: center;
}

.search-row input,
.search-row select {
  height: var(--kbx-input-height, 34px);
  padding: 4px 8px;
  border: 1px solid #d0d0d0;
  border-radius: 4px;
  font-size: var(--kbx-font-size, 14px);
}

.phase-filter,
.active-filter {
  flex: 0 0 140px;
}

.badge {
  background: var(--kbx-color-primary, #3b82f6);
  color: white;
  padding: 2px 6px;
  border-radius: 12px;
  font-size: 11px;
  margin-left: 4px;
}
</style>
