<template>
  <BatchOperationsPageV2 title="시장 데이터 수집 현황" subtitle="데이터 수집 작업 상태를 모니터링합니다." :state="state">
    <template #actions>
      <KsButton severity="secondary" label="새로고침" @click="refreshAll" />
    </template>

    <template #timeline>
      <div v-if="job">
        <div class="status-header">
          <h2>Job {{ job.jobId.substring(0, 8) }}</h2>
          <KsStatusTag :value="job.status" :severity="statusSeverity(job.status)" />
        </div>
        <div class="status-grid">
          <div class="stat"><span class="label">처리된 행</span><span class="value">{{ formatQuantity(job.rowsProcessed, 0) }}</span></div>
          <div class="stat"><span class="label">실패한 행</span><span class="value error">{{ formatQuantity(job.rowsFailed, 0) }}</span></div>
          <div class="stat"><span class="label">품질 점수</span><span class="value">{{ formatPercent(calculateQualityScore(job) / 100, 0) }}</span></div>
          <div v-if="job.durationSeconds" class="stat"><span class="label">소요 시간</span><span class="value">{{ job.durationSeconds }}초</span></div>
        </div>
        <p v-if="job.errorMessage" class="error-section"><strong>오류:</strong> {{ job.errorMessage }}</p>
      </div>
      <p v-else class="hint">수집 상태를 불러오는 중입니다...</p>
    </template>

    <template #records>
      <DataGridShell :rows="recentJobs" :columns="historyColumns" empty-message="최근 수집 이력이 없습니다." />
    </template>

    <template #reprocess>
      <p class="hint">진행 중이거나 대기 중인 작업이 있으면 10초 간격으로 자동 새로고침됩니다.</p>
    </template>
  </BatchOperationsPageV2>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import BatchOperationsPageV2 from '../../../shared/ui/screen-types/v2/BatchOperationsPageV2.vue'
import DataGridShell from '../../../shared/ui/DataGridShell.vue'
import { KsButton, KsStatusTag } from '../../../shared/ui/components'
import type { UiGridColumn, UiSeverity } from '../../../shared/ui/adapter/contracts'
import { formatAsOf, formatPercent, formatQuantity } from '../../../shared/formatters/financial'

interface IngestionJob {
  jobId: string
  status: string
  rowsProcessed: number
  rowsFailed: number
  rowsSkipped?: number
  durationSeconds?: number
  completedAt?: string
  errorMessage?: string
}

const job = ref<IngestionJob | null>(null)
const recentJobs = ref<IngestionJob[]>([])
const isLoading = ref(true)
const error = ref<string | null>(null)
const state = ref<'LOADING' | 'READY'>('LOADING')

const historyColumns: UiGridColumn[] = [
  { field: 'jobId', header: 'Job ID', formatter: value => String(value).substring(0, 8) },
  { field: 'status', header: '상태' },
  { field: 'rowsProcessed', header: '처리 행', formatter: value => formatQuantity(value as number, 0) },
  { field: 'durationSeconds', header: '소요 시간', formatter: value => value ? `${value}초` : '—' },
  { field: 'completedAt', header: '완료 시각', formatter: value => value ? formatAsOf(value as string) : '—' }
]

function statusSeverity(status: string): UiSeverity {
  const normalized = status.toLowerCase()
  if (normalized === 'completed') return 'success'
  if (normalized === 'failed') return 'danger'
  if (normalized === 'running') return 'info'
  return 'warning'
}

// Fetch latest job status from API
const fetchLatestJob = async () => {
  try {
    // In a real app, this would fetch from /api/market/ingest/latest
    // For now, we'll show a loading state
    const response = await fetch('/api/market/ingest/latest', {
      headers: {
        'X-KArtSell-User': 'ingestion-user',
        'X-KArtSell-Role': 'DataAdmin',
      },
    })

    if (response.ok) {
      job.value = await response.json()
    } else if (response.status === 404) {
      // No jobs yet - that's fine
      job.value = null
    } else {
      throw new Error(`API error: ${response.status}`)
    }
  } catch (err) {
    console.error('Failed to fetch latest job:', err)
    // Don't fail the page, just show no data
    job.value = null
  }
}

// Fetch recent jobs history
const fetchRecentJobs = async () => {
  try {
    const response = await fetch('/api/market/ingest/history?limit=10', {
      headers: {
        'X-KArtSell-User': 'ingestion-user',
        'X-KArtSell-Role': 'DataAdmin',
      },
    })

    if (response.ok) {
      recentJobs.value = await response.json()
    }
  } catch (err) {
    console.error('Failed to fetch recent jobs:', err)
    error.value = '수집 이력을 불러오지 못했습니다.'
  } finally {
    isLoading.value = false
    state.value = 'READY'
  }
}

function refreshAll() {
  state.value = 'LOADING'
  Promise.all([fetchLatestJob(), fetchRecentJobs()]).finally(() => { state.value = 'READY' })
}

onMounted(() => {
  fetchLatestJob()
  fetchRecentJobs()

  // Auto-refresh every 10 seconds if there's an active job
  const interval = setInterval(() => {
    if (job.value?.status === 'Running' || job.value?.status === 'Queued') {
      fetchLatestJob()
    }
  }, 10000)

  return () => clearInterval(interval)
})

const calculateQualityScore = (job: IngestionJob): number => {
  const total = job.rowsProcessed + job.rowsFailed + (job.rowsSkipped || 0)
  if (total === 0) return 0
  return Math.round((job.rowsProcessed / total) * 100)
}
</script>

<style scoped>
.status-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--ks-space-4); }
.status-header h2 { margin: 0; font-size: var(--ks-font-section); }
.status-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); gap: var(--ks-space-3); }
.stat { display: flex; flex-direction: column; gap: var(--ks-space-1); }
.stat .label { font-size: var(--ks-font-caption); color: var(--ks-color-text-muted); }
.stat .value { font-size: var(--ks-font-page); font-weight: 600; }
.stat .value.error { color: var(--ks-color-danger); }
.error-section { margin-top: var(--ks-space-3); padding: var(--ks-space-3); background: color-mix(in srgb, var(--ks-color-danger) 8%, var(--ks-color-surface)); border-left: 4px solid var(--ks-color-danger); border-radius: var(--ks-radius-sm); }
.hint { color: var(--ks-color-text-muted); font-size: var(--ks-font-caption); }
</style>
