"use client";

import { useRouter } from "next/navigation";
import { useEffect } from "react";

import { getDrawResults } from "@/api/draw";

/** 旧 `/results/check` 路由：跳转到最新期详情并展开查奖面板 */
export function CheckWinningRedirect() {
  const router = useRouter();

  useEffect(() => {
    void (async () => {
      try {
        const res = await getDrawResults({ page: 1, size: 1 });
        const drawNo = res.items[0]?.draw_no;
        if (drawNo) {
          router.replace(`/results/${encodeURIComponent(drawNo)}?check=1`);
          return;
        }
      } catch {
        /* 回落到列表 */
      }
      router.replace("/results");
    })();
  }, [router]);

  return null;
}