/**
 * Smoke test: real Aliyun docmind API call using createReadStream.
 * Run: node --experimental-strip-types scripts/smoke-docmind.ts <pdf-path>
 */
import DocmindClient from "@alicloud/docmind-api20220711";
import { RuntimeOptions } from "@alicloud/tea-util";
import { createReadStream } from "node:fs";
import { basename } from "node:path";

const accessKeyId = process.env["ALIBABA_CLOUD_ACCESS_KEY_ID"];
const accessKeySecret = process.env["ALIBABA_CLOUD_ACCESS_KEY_SECRET"];
if (accessKeyId === undefined || accessKeySecret === undefined) {
  console.error("Set ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET env vars.");
  process.exit(1);
}

const pdfPath = process.argv[2] ?? "../examples/trial/prime_sieve.pdf";
console.log(`Parsing: ${pdfPath}`);

const client = new DocmindClient.default({
  endpoint: "docmind-api.cn-hangzhou.aliyuncs.com",
  accessKeyId,
  accessKeySecret,
  type: "access_key",
  regionId: "cn-hangzhou",
} as never);

const fileStream = createReadStream(pdfPath);
const runtime = new RuntimeOptions({});

console.log("Submitting job...");
const submitResp = await client.submitDocParserJobAdvance(
  new DocmindClient.SubmitDocParserJobAdvanceRequest({
    fileUrlObject: fileStream,
    fileName: basename(pdfPath),
    outputFormat: ["markdown"],
    formulaEnhancement: true,
  }),
  runtime,
);
const jobId = submitResp.body?.data?.id;
console.log("Job ID:", jobId);

if (jobId === undefined) {
  console.error("No job id:", JSON.stringify(submitResp.body));
  process.exit(1);
}

console.log("Polling...");
const deadline = Date.now() + 5 * 60_000;
let status = "";
let markdownUrl = "";
let pageCount = 0;
while (Date.now() < deadline) {
  await new Promise((r) => setTimeout(r, 10_000));
  const resp = await client.queryDocParserStatus(
    new DocmindClient.QueryDocParserStatusRequest({ id: jobId }),
  );
  const data = (resp.body as { data?: { status?: string; pageCountEstimate?: number; outputFormatResult?: Array<{ outputFileUrl?: string; outputType?: string }> } }).data;
  status = data?.status ?? "";
  console.log(`  status: ${status}`);
  if (status === "success") {
    const md = data?.outputFormatResult?.find((r) => r.outputType === "markdown");
    markdownUrl = md?.outputFileUrl ?? "";
    pageCount = data?.pageCountEstimate ?? 0;
    break;
  }
  if (status === "fail") {
    console.error("Job failed!");
    process.exit(1);
  }
}

if (status !== "success" || markdownUrl === "") {
  console.error("Failed or timed out:", status);
  process.exit(1);
}

console.log("Downloading markdown from OSS...");
const mdResp = await fetch(markdownUrl);
const markdown = await mdResp.text();

console.log("--- Result ---");
console.log("Pages:", pageCount);
console.log("Cost (USD, est):", ((pageCount > 0 ? pageCount : 1) * 0.0056).toFixed(4));
console.log("Job ID:", jobId);
console.log("Markdown length:", markdown.length, "chars");
console.log("--- Markdown (first 1000 chars) ---");
console.log(markdown.slice(0, 1000));
