<script setup>
import { ref } from "vue";
import { storeToRefs } from "pinia";
import { useWorkflowStore, executeVideoRewrite } from "../../stores/workflow.js";

const workflow = useWorkflowStore();
const {
  videoLinkModalVisible,
  videoShareText,
  videoRewriting,
  rewriteProgress,
} = storeToRefs(workflow);

const feedback = ref({ severity: "", message: "" });

const tipItems = [
  "已支持抖音分享文本、抖音视频链接、抖音短链接",
  "支持直接视频文件 URL（如 .mp4、.mov 等）",
  "快手、小红书、B站、视频号分享链接目前会提示暂不支持",
  "流程：解析链接 → 下载视频 → 提取音频 → 语音识别 → AI 仿写",
];

function onCancel() {
  feedback.value = { severity: "", message: "" };
  if (videoRewriting.value) {
    workflow.videoRewriteAbortRequested = true;
    workflow.videoRewriting = false;
    workflow.rewriteProgress = "";
  }
  workflow.videoLinkModalVisible = false;
}

function onVisibleChange(visible) {
  if (visible) {
    workflow.videoLinkModalVisible = true;
    return;
  }
  onCancel();
}

async function onRewrite() {
  feedback.value = { severity: "", message: "" };
  const result = await executeVideoRewrite(workflow);
  if (result?.cancelled) return;
  if (!result.ok) {
    feedback.value = { severity: "error", message: result.message };
  } else {
    feedback.value = { severity: "success", message: result.message };
  }
}
</script>

<template>
  <Dialog
    v-model:visible="videoLinkModalVisible"
    modal
    header="粘贴视频分享文本 - 快速仿写"
    :style="{ width: '600px' }"
    :draggable="false"
    class="ip-brain-dialog"
    @update:visible="onVisibleChange"
  >
    <div class="space-y-4">
      <Message
        v-if="feedback.message"
        :severity="feedback.severity"
        :closable="false"
        class="w-full"
      >
        {{ feedback.message }}
      </Message>

      <Message
        v-if="videoRewriting && rewriteProgress"
        severity="info"
        :closable="false"
        class="w-full"
      >
        {{ rewriteProgress }}
      </Message>

      <div>
        <label class="mb-2 block font-medium text-slate-200">
          请粘贴视频分享文本或视频链接
        </label>
        <p class="mb-2 text-xs text-slate-500">
          支持抖音分享文本、抖音视频链接、抖音短链接，以及直接视频文件链接
        </p>
        <InputText
          v-model="videoShareText"
          type="text"
          size="large"
          class="w-full"
          placeholder="请输入分享文本、视频链接，或 mp4/mov/webm 等直链..."
          :disabled="videoRewriting"
          @keyup.enter="onRewrite"
        />
      </div>

      <div class="elegant-tip-box rounded-lg p-3">
        <div class="text-sm text-slate-300">
          <div class="mb-1 font-medium">提示:</div>
          <ul class="list-inside list-disc space-y-1">
            <li v-for="(tip, i) in tipItems" :key="i">{{ tip }}</li>
          </ul>
        </div>
      </div>
    </div>

    <template #footer>
      <Button
        label="取消"
        severity="secondary"
        :disabled="videoRewriting"
        @click="onCancel"
      />
      <Button label="一键仿写" :loading="videoRewriting" @click="onRewrite" />
    </template>
  </Dialog>
</template>
