<script setup lang="ts">
import { computed, onMounted, onBeforeUnmount, ref, watch } from "vue";
import { kubeflowService } from "@/components/service/management/kubeflowService";

type RunPayload = {
  display_name: string;
  description?: string;
  pipeline_version_reference: { pipeline_id: string };
  runtime_config?: { parameters?: Record<string, any> };
  service_account?: string;
};

const props = defineProps<{
  /** 테이블에서 선택된 파이프라인의 pipelineId */
  pipelineId?: string | number | null;
}>();

const emit = defineEmits<{
  (e: "close-modal"): void;
  (e: "submitted", value: any): void;
}>();

const form = ref({
  display_name: "", // ✅ 빈 값으로 시작
  description: "", // ✅ 빈 값으로 시작
  pipeline_id: "", // prop으로만 채움(읽기 전용)
});
const loading = ref(false);
const errorMsg = ref("");

const isValid = computed(
  () => !!form.value.display_name.trim() && !!form.value.pipeline_id.trim(),
);

function initForm() {
  form.value.pipeline_id = props.pipelineId ? String(props.pipelineId) : "";
  // display_name/description은 비워둠
}

onMounted(initForm);
watch(() => props.pipelineId, initForm);

function onEsc(e: KeyboardEvent) {
  if (e.key === "Escape" && !loading.value) emit("close-modal");
}
onMounted(() => window.addEventListener("keydown", onEsc));
onBeforeUnmount(() => window.removeEventListener("keydown", onEsc));

async function submitRun() {
  errorMsg.value = "";
  if (!isValid.value) {
    errorMsg.value = "Run 제목(display_name)과 pipeline_id는 필수입니다.";
    return;
  }

  const payload: RunPayload = {
    display_name: form.value.display_name.trim(),
    description: form.value.description?.trim(),
    pipeline_version_reference: { pipeline_id: form.value.pipeline_id.trim() },
    runtime_config: { parameters: {} }, // 필요 시 파라미터 매핑
    service_account: "pipeline-runner",
  };

  try {
    loading.value = true;
    const { data } = await kubeflowService.run(payload);
    emit("submitted", data);
    emit("close-modal");
  } catch (e: any) {
    console.error("Run 생성 실패:", e);
    const msg =
      e?.response?.data?.message ||
      e?.response?.data?.error ||
      e?.message ||
      "Run 생성에 실패했습니다.";
    errorMsg.value = String(msg);
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <v-card>
    <v-card-title
      class="text-white font-weight-bold text-h6"
      style="background-color: #1976d2"
    >
      Run Pipeline
    </v-card-title>

    <v-card-text class="pa-6">
      <v-form @submit.prevent="submitRun">
        <!-- 제목 -->
        <div class="mb-4">
          <label class="text-subtitle-2 font-weight-medium mb-1 d-block">
            Run Title (display_name)
          </label>
          <v-text-field
            v-model="form.display_name"
            variant="outlined"
            :disabled="loading"
            density="comfortable"
            hide-details="auto"
            persistent-hint
            required
          />
        </div>

        <!-- 내용 -->
        <div class="mb-4">
          <label class="text-subtitle-2 font-weight-medium mb-1 d-block">
            Run Description
          </label>
          <v-textarea
            v-model="form.description"
            variant="outlined"
            :disabled="loading"
            rows="3"
            density="comfortable"
            hide-details="auto"
          />
        </div>

        <!-- pipeline_id -->
        <div class="mb-2">
          <label class="text-subtitle-2 font-weight-medium mb-1 d-block">
            pipeline_id
          </label>
          <v-text-field
            v-model="form.pipeline_id"
            variant="outlined"
            :disabled="true"
            density="comfortable"
            hide-details="auto"
            required
          />
        </div>

        <div v-if="errorMsg" class="mt-3 text-error">{{ errorMsg }}</div>
      </v-form>
    </v-card-text>

    <v-card-actions class="justify-end" style="padding: 16px 24px">
      <v-btn
        color="success"
        :loading="loading"
        :disabled="!isValid"
        @click="submitRun"
      >
        RUN
      </v-btn>
      <v-btn text :disabled="loading" @click="$emit('close-modal')"
        >CLOSE</v-btn
      >
    </v-card-actions>
  </v-card>
</template>
