<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import PageCard from '@/components/PageCard.vue'
import EvalTaskSetupStep, { type EvalTaskSetupDraft } from './create/EvalTaskSetupStep.vue'
import EvalRuleSetupStep, { type EvalRuleSetupDraft } from './create/EvalRuleSetupStep.vue'
import { createDimension, getDimensionList, startEval } from '@/api/modules/eval'
import { getTrainedModels, getModelList } from '@/api/modules/model'
import { getDatasetList } from '@/api/modules/dataset'
import { getSystemInfo } from '@/api/modules/system'
import type { DatasetItem, Dimension, GpuInfo, ModelItem, TrainedModel } from '@/types'

type StepExposed = { validate: () => Promise<boolean> }

const router = useRouter()
const loading = ref(false)
const submitting = ref(false)
const taskStepRef = ref<StepExposed>()
const ruleStepRef = ref<StepExposed>()

const trainedModels = ref<TrainedModel[]>([])
const evalDatasets = ref<DatasetItem[]>([])
const dimensions = ref<Dimension[]>([])
const evalModels = ref<ModelItem[]>([])
const gpus = ref<GpuInfo[]>([])
const createdDimensionId = ref<string | number>('')

const taskForm = ref<EvalTaskSetupDraft>({
  eval_task_name: '',
  model_id: '',
  gpu_id: '',
  data_source: 'dataset',
  dataset_id: '',
  leaderboard: false,
})

const ruleForm = ref<EvalRuleSetupDraft>({
  ruleMode: 'existing',
  dimension_id: '',
  newDimension: {
    name: '',
    type: '',
    description: '',
    eval_model: '',
    eval_method: '',
    eval_prompt: '',
    is_active: true,
    is_default: false,
    bleu_n: 1,
    output_precision: 3,
    score_min: 0,
    score_max: 5,
    pass_threshold: 3,
  },
})

watch(
  () => ruleForm.value.newDimension,
  () => {
    createdDimensionId.value = ''
  },
  { deep: true },
)

async function loadData() {
  loading.value = true
  const results = await Promise.allSettled([
    getTrainedModels(),
    getDimensionList(),
    getDatasetList(),
    getSystemInfo(),
    getModelList(),
  ])

  if (results[0].status === 'fulfilled') trainedModels.value = results[0].value?.models || []
  if (results[1].status === 'fulfilled') dimensions.value = results[1].value || []
  if (results[2].status === 'fulfilled') {
    evalDatasets.value = (results[2].value || []).filter((dataset) => dataset.type === 'eval')
  }
  if (results[3].status === 'fulfilled') gpus.value = results[3].value?.gpu || []
  if (results[4].status === 'fulfilled') {
    evalModels.value = (results[4].value || []).filter((model) => model.purpose === 'evaluation')
  }

  const failedCount = results.filter((result) => result.status === 'rejected').length
  if (failedCount > 0) {
    ElMessage.error(`部分基础数据加载失败（${failedCount} 项），请刷新页面后重试`)
  }
  loading.value = false
}



function buildDimensionPayload() {
  const draft = ruleForm.value.newDimension
  const payload: Partial<Dimension> = {
    name: draft.name,
    type: draft.type,
    description: draft.description,
    eval_model: draft.type === 'text_similarity' ? undefined : draft.eval_model,
    eval_method: draft.eval_method,
    eval_prompt: draft.type === 'text_similarity' ? undefined : draft.eval_prompt,
    is_active: draft.is_active,
    is_default: draft.is_default,
    create_time: new Date().toISOString(),
  }
  if (draft.type === 'text_similarity') {
    payload.bleu_n = draft.bleu_n
    payload.output_precision = draft.output_precision
  }
  if (draft.type === 'metric') {
    payload.score_min = draft.score_min
    payload.score_max = draft.score_max
    payload.pass_threshold = draft.pass_threshold
  }
  return payload
}

async function resolveDimensionId() {
  if (ruleForm.value.ruleMode === 'baseline') return ''
  if (ruleForm.value.ruleMode === 'existing') return ruleForm.value.dimension_id
  if (createdDimensionId.value !== '') return createdDimensionId.value

  const created = await createDimension(buildDimensionPayload())
  if (created?.id === undefined || created.id === null || created.id === '') {
    throw new Error('评测维度已提交，但未返回维度 ID，无法启动评测任务')
  }
  createdDimensionId.value = created.id
  return created.id
}

async function handleSubmit() {
  if (loading.value || submitting.value) return
  const taskValid = await taskStepRef.value?.validate()
  const ruleValid = await ruleStepRef.value?.validate()
  if (!taskValid || !ruleValid) {
    ElMessage.warning('请检查并完善所有必填项')
    return
  }

  submitting.value = true
  try {
    const dimensionId = await resolveDimensionId()
    await startEval({
      eval_task_name: taskForm.value.eval_task_name,
      eval_type: ruleForm.value.ruleMode === 'baseline' ? 'baseline' : 'custom',
      model_id: taskForm.value.model_id,
      gpu_id: taskForm.value.gpu_id,
      dataset_id: taskForm.value.data_source === 'dataset' ? taskForm.value.dataset_id : '',
      dimension_id: dimensionId,
      data_source: taskForm.value.data_source,
      leaderboard: taskForm.value.leaderboard,
    })
    ElMessage.success('评测任务已创建并启动')
    router.push('/model-eval')
  } catch (error) {
    const message = error instanceof Error ? error.message : '创建评测任务失败，请稍后重试'
    ElMessage.error(message)
  } finally {
    submitting.value = false
  }
}

function handleCancel() {
  router.back()
}

onMounted(loadData)
</script>

<template>
  <PageCard title="新建评测任务" subtitle="完成任务配置与评测规则设置后启动评测">
    <div v-loading="loading" class="form-container">
      <div class="section-card">
        <div class="section-title">任务基本配置</div>
        <EvalTaskSetupStep
          ref="taskStepRef"
          v-model="taskForm"
          :trained-models="trainedModels"
          :eval-datasets="evalDatasets"
          :gpus="gpus"
          :loading="loading"
          :disabled="submitting"
        />
      </div>

      <div class="section-card">
        <div class="section-title">评测规则设置</div>
        <EvalRuleSetupStep
          ref="ruleStepRef"
          v-model="ruleForm"
          :dimensions="dimensions"
          :eval-models="evalModels"
          :loading="loading"
          :disabled="submitting"
        />
      </div>
    </div>

    <div class="form-footer">
      <el-button :disabled="submitting" @click="handleCancel">取消</el-button>
      <el-button
        type="primary"
        :loading="submitting"
        :disabled="loading"
        @click="handleSubmit"
      >
        创建并启动评测
      </el-button>
    </div>
  </PageCard>
</template>

<style scoped>
.form-container {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.section-card {
  padding: 24px;
  border-radius: 8px;
  background: #f9fafb;
  border: 1px solid #e5e7eb;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #111827;
  margin-bottom: 24px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e5e7eb;
}

.form-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid #e5e7eb;
}
</style>
