<script setup lang="ts">
import { ref, computed, reactive } from 'vue'
import BatchOperationsPageV2 from '../../../shared/ui/screen-types/v2/BatchOperationsPageV2.vue'
import DataGridShell from '../../../shared/ui/DataGridShell.vue'
import { KsButton } from '../../../shared/ui/components'
import type { StandardScreenState } from '../../../shared/ui/contracts/screenContract'
import type { UiGridColumn } from '../../../shared/ui/adapter/contracts'
import { useShadowRunsList } from '../composables/useShadowRuns'

const currentPage = ref(1)
const pageSize = ref(20)

const evidence = reactive({
  asOf: '2026-08-15T19:40:00Z',
  version: 'v60-T08-Contract',
})

const queryParams = computed(() => ({
  page: currentPage.value,
  pageSize: pageSize.value,
}))

const shadowRunsQuery = useShadowRunsList(queryParams.value)

const screenState = computed<StandardScreenState>(() => {
  if (shadowRunsQuery.isPending.value) return 'LOADING'
  if (shadowRunsQuery.isError.value) return 'ERROR'
  const data = shadowRunsQuery.data.value as any
  if (!data?.items?.length) return 'EMPTY'
  return 'READY'
})

const items = computed(() => {
  const data = shadowRunsQuery.data.value as any
  return data?.items || []
})

const columns: UiGridColumn[] = [
  { field: 'runId', header: 'Run ID', minWidth: 120, flex: 2, formatter: val => String(val).startsWith('#') ? String(val) : `#${String(val).slice(0, 8)}...` },
  { field: 'modelName', header: 'Model Name', minWidth: 200, flex: 3 },
  { field: 'status', header: 'Status', minWidth: 100, flex: 1, formatter: val => String(val).toUpperCase() },
  { field: 'pbo', header: 'PBO', minWidth: 80, flex: 1, formatter: val => `${val}%` },
  { field: 'dsr', header: 'DSR', minWidth: 80, flex: 1, formatter: val => `${val}%` },
  { field: 'oos', header: 'OOS', minWidth: 80, flex: 1, formatter: val => `${val}%` },
]

const handleRetry = () => {
  shadowRunsQuery.refetch()
}
</script>

<template>
  <BatchOperationsPageV2
    title="Shadow Run 실행 검증 (Shadow Run Validation)"
    subtitle="252+ 거래일 백테스트 검증 결과를 관찰하고 통계적 우위(PBO/DSR/OOS)를 평가합니다."
    :state="screenState"
    :evidence="evidence"
    :error="shadowRunsQuery.error.value ?? undefined"
    @retry="handleRetry"
  >
    <template #actions>
      <KsButton
        label="▶ 실행 검증 재요청"
        variant="primary"
        aria-label="Shadow Run 검증 실행"
      />
    </template>

    <DataGridShell
      :rows="items"
      :columns="columns"
      :loading="shadowRunsQuery.isPending.value"
      empty-message="조회된 Shadow Run 검증 내역이 없습니다."
      height="100%"
    />
  </BatchOperationsPageV2>
</template>

