<script setup lang="ts">
import { ref, reactive, computed, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import PageCard from '@/components/PageCard.vue'
import { startEval } from '@/api/modules/eval'
import { getDimensionList } from '@/api/modules/eval'
import { getTrainedModels } from '@/api/modules/model'
import { getDatasetList } from '@/api/modules/dataset'
import { getSystemInfo } from '@/api/modules/system'
import type { Dimension, TrainedModel, DatasetItem, GpuInfo } from '@/types'

const router = useRouter()
const formRef = ref<FormInstance>()
const submitting = ref(false)

const trainedModels = ref<TrainedModel[]>([])
const evalDatasets = ref<DatasetItem[]>([])
const dimensions = ref<Dimension[]>([])
const gpus = ref<GpuInfo[]>([])

const form = reactive({
  eval_task_name: '',
  eval_type: 'custom' as 'custom' | 'baseline',
  model_id: '' as string | number,
  gpu_id: '',
  data_source: 'dataset' as 'dataset' | 'inference',
  dataset_id: '' as string | number,
  dimension_id: '' as string | number,
  leaderboard: false, // 修复原项目字段名 bug（原读 leader 恒 false）
})

const rules: FormRules = {
  eval_task_name: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
  model_id: [{ required: true, message: '请选择评测模型', trigger: 'change' }],
  gpu_id: [{ required: true, message: '请选择 GPU', trigger: 'change' }],
  dimension_id: [{ required: true, message: '请选择评测维度', trigger: 'change' }],
}

const showEvalRules = computed(() => form.eval_type === 'custom')
const showDataset = computed(() => form.data_source === 'dataset')

/** 选中维度详情 */
const selectedDimension = computed(() =>
  dimensions.value.find((d) => d.id == form.dimension_id),
)

async function loadData() {
  try {
    const [models, dims, allDs, sys] = await Promise.all([
      getTrainedModels(),
      getDimensionList(),
      getDatasetList(),
      getSystemInfo(),
    ])
    trainedModels.value = models?.models || []
    dimensions.value = dims || []
    evalDatasets.value = (allDs || []).filter((d: DatasetItem) => d.type === 'eval')
    gpus.value = sys?.gpu || []
  } catch {
    // ignore
  }
}

async function handleSubmit() {
  if (!formRef.value) return
  await formRef.value.validate(async (valid) => {
    if (!valid) return
    submitting.value = true
    try {
      await startEval({
        eval_task_name: form.eval_task_name,
        eval_type: form.eval_type,
        model_id: form.model_id,
        gpu_id: form.gpu_id,
        dataset_id: form.dataset_id,
        dimension_id: form.dimension_id,
        data_source: form.data_source,
        leaderboard: form.leaderboard,
      })
      ElMessage.success('评测任务已启动')
      router.push('/model-eval')
    } catch {
      // ignore
    } finally {
      submitting.value = false
    }
  })
}

function handleCancel() {
  router.back()
}

onMounted(loadData)
</script>

<template>
  <PageCard title="新建评测">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px" style="max-width: 720px">
      <el-form-item label="任务名称" prop="eval_task_name">
        <el-input v-model="form.eval_task_name" placeholder="请输入任务名称" maxlength="50" show-word-limit />
      </el-form-item>

      <el-form-item label="评测方式">
        <el-radio-group v-model="form.eval_type">
          <el-radio-button value="custom">自定义评测</el-radio-button>
          <el-radio-button value="baseline">基线评测</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="评测模型" prop="model_id">
        <el-select v-model="form.model_id" placeholder="请选择已训练模型" filterable style="width: 100%">
          <el-option
            v-for="m in trainedModels"
            :key="m.id"
            :label="m.name"
            :value="m.id"
            :disabled="!m.merged"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="选择 GPU" prop="gpu_id">
        <el-select v-model="form.gpu_id" placeholder="请选择 GPU" style="width: 100%">
          <el-option v-for="(g, idx) in gpus" :key="idx" :label="`${g.name} (GPU ${idx})`" :value="idx" />
        </el-select>
      </el-form-item>

      <el-form-item label="数据来源">
        <el-radio-group v-model="form.data_source">
          <el-radio value="dataset">评测数据集</el-radio>
          <el-radio value="inference">推理结果集</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="showDataset" label="评测数据集" prop="dataset_id">
        <el-select v-model="form.dataset_id" placeholder="请选择评测数据集" filterable style="width: 100%">
          <el-option v-for="d in evalDatasets" :key="d.id" :label="d.name" :value="d.id" />
        </el-select>
      </el-form-item>

      <!-- 评测规则（仅 custom 显示） -->
      <template v-if="showEvalRules">
        <el-divider content-position="left">评测规则</el-divider>
        <el-form-item label="评测维度" prop="dimension_id">
          <el-select
            v-model="form.dimension_id"
            placeholder="请选择评测维度"
            filterable
            style="width: 100%"
          >
            <el-option v-for="d in dimensions" :key="d.id" :label="d.name" :value="d.id" />
          </el-select>
        </el-form-item>

        <el-form-item v-if="selectedDimension" label="维度详情">
          <el-descriptions :column="1" border size="small">
            <el-descriptions-item label="评估模型">{{ selectedDimension.eval_model || '-' }}</el-descriptions-item>
            <el-descriptions-item label="评估方法">
              {{ Array.isArray(selectedDimension.eval_method) ? selectedDimension.eval_method.join(', ') : selectedDimension.eval_method || '-' }}
            </el-descriptions-item>
          </el-descriptions>
        </el-form-item>
      </template>

      <el-form-item label="加入排行榜">
        <el-switch v-model="form.leaderboard" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :loading="submitting" @click="handleSubmit">启动评测</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </PageCard>
</template>
