import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";
import fs from "fs";
import path from "path";

const prisma = new PrismaClient();

// --- Inline BibTeX parser (mirrors src/lib/bibtex-parser.ts) ---
function parseBibtex(input: string) {
  const entries: Array<{
    title: string;
    authors: string;
    venue: string;
    year: number;
    url: string | null;
    pdfUrl: string | null;
    doiUrl: string | null;
    codeUrl: string | null;
    videoUrl: string | null;
  }> = [];

  const entryRegex = /@(\w+)\s*\{([^,]*),/g;
  let match: RegExpExecArray | null;

  while ((match = entryRegex.exec(input)) !== null) {
    const startIdx = match.index + match[0].length;
    let depth = 1;
    let endIdx = startIdx;
    for (let i = startIdx; i < input.length && depth > 0; i++) {
      if (input[i] === "{") depth++;
      else if (input[i] === "}") depth--;
      if (depth === 0) endIdx = i;
    }

    const body = input.slice(startIdx, endIdx);
    const fields = parseFields(body);

    const title = cleanValue(fields["title"] || "");
    if (!title) continue;

    const authors = cleanValue(fields["author"] || "").replace(/\s+and\s+/g, ", ");
    const venue = cleanValue(fields["journal"] || "") || cleanValue(fields["booktitle"] || "");
    const yearStr = cleanValue(fields["year"] || "");
    const year = parseInt(yearStr, 10) || new Date().getFullYear();

    const doiRaw = cleanValue(fields["doi"] || "");
    const doiUrl = doiRaw
      ? doiRaw.startsWith("http") ? doiRaw : `https://doi.org/${doiRaw}`
      : null;

    entries.push({
      title,
      authors,
      venue,
      year,
      url: cleanValue(fields["url"] || "") || null,
      pdfUrl: cleanValue(fields["pdf"] || "") || null,
      doiUrl,
      codeUrl: cleanValue(fields["code"] || "") || null,
      videoUrl: cleanValue(fields["video"] || "") || null,
    });
  }
  return entries;
}

function parseFields(body: string): Record<string, string> {
  const fields: Record<string, string> = {};
  const fieldRegex = /(\w+)\s*=\s*/g;
  let m: RegExpExecArray | null;
  while ((m = fieldRegex.exec(body)) !== null) {
    const fieldName = m[1].toLowerCase();
    const valueStart = m.index + m[0].length;
    const value = extractValue(body, valueStart);
    if (value !== null) fields[fieldName] = value;
  }
  return fields;
}

function extractValue(body: string, start: number): string | null {
  const ch = body[start];
  if (ch === "{") {
    let depth = 1;
    let i = start + 1;
    for (; i < body.length && depth > 0; i++) {
      if (body[i] === "{") depth++;
      else if (body[i] === "}") depth--;
    }
    return body.slice(start + 1, i - 1);
  } else if (ch === '"') {
    const end = body.indexOf('"', start + 1);
    if (end === -1) return null;
    return body.slice(start + 1, end);
  } else {
    const end = body.indexOf(",", start);
    if (end === -1) return body.slice(start).trim();
    return body.slice(start, end).trim();
  }
}

function cleanValue(value: string): string {
  return value.replace(/[{}]/g, "").replace(/\s+/g, " ").trim();
}

async function main() {
  const adminPassword = process.env.ADMIN_PASSWORD || "admin1234";
  const hashedPassword = await bcrypt.hash(adminPassword, 10);

  await prisma.admin.upsert({
    where: { username: "admin" },
    update: {},
    create: { username: "admin", password: hashedPassword },
  });

  await prisma.siteConfig.upsert({
    where: { id: "main" },
    update: {},
    create: {
      labName: "Software Engineering Laboratory",
      tagline: "Hanyang University SELab",
      description:
        "소프트웨어 공학의 핵심 문제를 해결하고, 더 나은 소프트웨어 개발 방법론을 연구합니다.",
      address: "서울특별시 성동구 왕십리로 222",
      building: "한양대학교 ITBT 614호",
      email: "yunhokim@hanyang.ac.kr",
      phone: "02-2220-2385",
      mapUrl: "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d559.1386440000607!2d127.0490294341321!3d37.55586919885408!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x357ca59a112efe17%3A0x6c8e2441f4b2ff7e!2z7ZWc7JaR64yA7ZWZ6rWQIElUQlTqtIA!5e0!3m2!1sko!2skr!4v1774328127956!5m2!1sko!2skr",
      aboutContent:
        "SELab은 소프트웨어 공학 분야의 다양한 연구를 수행하는 연구실입니다. 소프트웨어 개발 프로세스, 품질 보증, 테스팅, 유지보수 등의 주제를 중심으로 실용적이고 혁신적인 연구를 진행하고 있습니다.",
      joinUsContent:
        "SELab에서는 열정적인 대학원생 및 인턴을 모집하고 있습니다. 소프트웨어 공학에 관심이 있는 분은 언제든지 연락해 주세요.",
    },
  });

  const researchAreas = [
    {
      title: "Software Testing",
      description:
        "자동화된 테스트 생성, 회귀 테스트, 뮤테이션 테스팅 등 소프트웨어 품질을 보장하기 위한 다양한 테스팅 기법을 연구합니다.",
      sortOrder: 1,
    },
    {
      title: "Software Maintenance",
      description:
        "코드 리팩토링, 기술 부채 관리, 소프트웨어 진화 등 소프트웨어의 장기적인 유지보수성을 향상시키는 방법을 연구합니다.",
      sortOrder: 2,
    },
    {
      title: "AI for Software Engineering",
      description:
        "인공지능과 머신러닝 기술을 소프트웨어 공학에 적용하여 개발 생산성과 코드 품질을 높이는 연구를 수행합니다.",
      sortOrder: 3,
    },
  ];

  for (const area of researchAreas) {
    await prisma.research.create({ data: area });
  }

  const members = [
    {
      name: "김윤호",
      nameEn: "Yunho Kim",
      role: "professor",
      bio: "한양대학교 컴퓨터소프트웨어학부 조교수",
      interest: "",
      email: "yunhokim@hanyang.ac.kr",
      homepage: "https://yunho-kim.github.io/",
      scholar: "https://scholar.google.com/citations?user=kkT03G0AAAAJ&hl=ko&oi=ao",
      authorAliases: JSON.stringify(["Yunho Kim", "Kim, Yunho"]),
      sortOrder: 0,
    },
    {
      name: "모현민",
      nameEn: "Hyeonmin Mo",
      role: "msphd",
      interest: "",
      email: "hyeonminmo@hanyang.ac.kr",
      github: "https://github.com/hyeonminmo",
      scholar: "https://scholar.google.com/citations?user=9s7ppLEAAAAJ&hl=ko&oi=sra",
      authorAliases: JSON.stringify(["Hyeonmin Mo"]),
      sortOrder: 1,
    },
    {
      name: "정지나",
      nameEn: "Gina Jung",
      role: "msphd",
      interest: "",
      email: "snowgina@hanyang.ac.kr",
      github: "https://github.com/jung-gina",
      scholar: "",
      authorAliases: JSON.stringify(["Gina Jung"]),
      sortOrder: 2,
    },
    {
      name: "양종문",
      nameEn: "Jongmun Yang",
      role: "msphd",
      interest: "",
      email: "jongmunyang@hanyang.ac.kr",
      github: "https://github.com/sheepbelldoor",
      scholar: "https://scholar.google.com/citations?user=UEOC_j4AAAAJ&hl=ko&oi=ao",
      authorAliases: JSON.stringify(["Jongmun Yang"]),
      sortOrder: 3,
    },
  ];

  for (const m of members) {
    await prisma.member.create({ data: m });
  }

  // --- Test post ---
  await prisma.post.create({
    data: {
      title: "SELab 홈페이지가 오픈!",
      content: "안녕하세요, Software Engineering Laboratory 홈페이지가 새롭게 오픈되었습니다. 많은 관심 부탁드립니다.",
      category: "notice",
      published: true,
      pinned: false,
    },
  });

  // --- Import publications from publish.bib ---
  const bibPath = path.join(__dirname, "..", "publish.bib");
  if (fs.existsSync(bibPath)) {
    const bibContent = fs.readFileSync(bibPath, "utf-8");
    const publications = parseBibtex(bibContent);
    for (const pub of publications) {
      await prisma.publication.create({ data: pub });
    }
    console.log(`Imported ${publications.length} publications from publish.bib`);
  } else {
    console.log("publish.bib not found, skipping publication import");
  }

  console.log("Seed data created successfully");
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(() => prisma.$disconnect());
