// Shared mock data for the Academic Advisor prototype.
// These match the schema shapes in the backend (courses.json, degree_requirements.json).

async function fetchCurrentUser() {
  const token = localStorage.getItem("token");
  if (!token) {
    window.location.href = "/login.html";
    return null;
  }
  const res = await fetch(`${window.API_BASE}/api/profile`, {
    headers: { "Authorization": "Bearer " + token },
  });
  if (!res.ok) {
    window.location.href = "/login.html";
    return null;
  }
  const user = await res.json();
  return {
    id: user.id,
    name: user.name,
    initials: user.name.split(" ").map(w => w[0]).join("").toUpperCase(),
    email: user.email,
    studentType: user.student_type || "undergraduate",
    major: user.major || "Not set",
    year: user.year || "Not set",
    expectedGrad: user.expected_grad || "Not set",
    completedCourses: user.completed_courses || [],
    inProgressCourses: user.in_progress_courses || [],
  };
}

const COURSES = [
  { code: "CSCE 145", title: "Algorithmic Design I", credits: 4, desc: "Problem-solving using structured design and Java; expressions, control structures, arrays, methods.", prereqs: [], tags: ["Core", "Freshman"], seats: 22, offered: ["Fall", "Spring"] },
  { code: "CSCE 146", title: "Algorithmic Design II", credits: 4, desc: "Object-oriented design, recursion, basic data structures, and algorithm analysis.", prereqs: ["CSCE 145"], tags: ["Core"], seats: 14, offered: ["Fall", "Spring"] },
  { code: "CSCE 240", title: "Advanced Programming Techniques", credits: 3, desc: "Programming in C/C++, pointers, memory management, and Unix tools.", prereqs: ["CSCE 146"], tags: ["Core"], seats: 8, offered: ["Fall", "Spring"] },
  { code: "CSCE 247", title: "Software Engineering Foundations", credits: 3, desc: "Requirements, design, testing, version control, and team-based software construction.", prereqs: ["CSCE 146"], tags: ["Core"], seats: 19, offered: ["Fall"] },
  { code: "CSCE 311", title: "Operating Systems", credits: 3, desc: "Processes, threads, synchronization, memory management, and file systems.", prereqs: ["CSCE 240"], tags: ["Core", "Systems"], seats: 5, offered: ["Fall", "Spring"] },
  { code: "CSCE 330", title: "Programming Language Structures", credits: 3, desc: "Functional, logic, and declarative paradigms; interpreters and type systems.", prereqs: ["CSCE 240"], tags: ["Theory"], seats: 11, offered: ["Spring"] },
  { code: "CSCE 350", title: "Data Structures and Algorithms", credits: 3, desc: "Trees, graphs, hashing, sorting; asymptotic complexity; divide-and-conquer and greedy algorithms.", prereqs: ["CSCE 240", "MATH 374"], tags: ["Core", "Theory"], seats: 9, offered: ["Fall", "Spring"] },
  { code: "CSCE 580", title: "Artificial Intelligence", credits: 3, desc: "Search, knowledge representation, planning, and intro to machine learning.", prereqs: ["CSCE 350"], tags: ["AI", "Elective"], seats: 6, offered: ["Fall"] },
  { code: "CSCE 585", title: "Machine Learning Systems", credits: 3, desc: "End-to-end ML pipelines: data, training, deployment, monitoring, and evaluation.", prereqs: ["CSCE 580"], tags: ["AI", "Elective", "Capstone-eligible"], seats: 3, offered: ["Spring"] },
  { code: "MATH 374", title: "Discrete Structures", credits: 3, desc: "Logic, sets, induction, combinatorics, graph theory.", prereqs: ["MATH 141"], tags: ["Math"], seats: 18, offered: ["Fall", "Spring"] },
  { code: "STAT 509", title: "Statistics for Engineers", credits: 3, desc: "Probability, distributions, inference, regression; applied projects.", prereqs: ["MATH 142"], tags: ["Math", "Analytics"], seats: 15, offered: ["Fall", "Spring"] },
  { code: "PHIL 318", title: "Ethics in the Professions", credits: 3, desc: "Case-based ethics for engineers and computing professionals; writing intensive.", prereqs: [], tags: ["Gen-Ed", "Carolina Core"], seats: 24, offered: ["Fall", "Spring"] },
];

const COMPLETED = ["CSCE 145", "CSCE 146", "CSCE 240", "CSCE 247", "MATH 141", "MATH 142", "MATH 374", "ENGL 101", "ENGL 102", "PSYC 101", "HIST 111"];

const PLAN = [
  {
    term: "Fall '25",
    status: "completed",
    courses: [
      { code: "CSCE 240", title: "Advanced Programming", cr: 3 },
      { code: "CSCE 247", title: "Software Engineering", cr: 3 },
      { code: "MATH 374", title: "Discrete Structures", cr: 3 },
      { code: "ENGL 102", title: "Rhetoric & Composition", cr: 3 },
    ],
  },
  {
    term: "Spring '26",
    status: "current",
    courses: [
      { code: "CSCE 311", title: "Operating Systems", cr: 3 },
      { code: "CSCE 350", title: "Data Structures & Algo.", cr: 3 },
      { code: "STAT 509", title: "Statistics for Engineers", cr: 3 },
      { code: "PHIL 318", title: "Ethics in the Professions", cr: 3 },
    ],
  },
  {
    term: "Fall '26",
    status: "planned",
    courses: [
      { code: "CSCE 580", title: "Artificial Intelligence", cr: 3 },
      { code: "CSCE 330", title: "Programming Lang. Struct.", cr: 3 },
    ],
  },
  {
    term: "Spring '27",
    status: "planned",
    courses: [
      { code: "CSCE 585", title: "ML Systems (Capstone)", cr: 3 },
    ],
  },
  { term: "Fall '27", status: "planned", courses: [] },
  { term: "Spring '28", status: "planned", courses: [] },
  { term: "Fall '28", status: "planned", courses: [] },
  { term: "Spring '29", status: "planned", courses: [] },
];

const REQUIREMENTS = [
  { label: "Carolina Core", completed: 28, total: 31 },
  { label: "Major requirements", completed: 34, total: 54 },
  { label: "Minor — Data Analytics", completed: 9, total: 18 },
  { label: "Free electives", completed: 7, total: 17 },
];

const HISTORY = [
  { id: "h1", title: "Can I take CSCE 585 next spring?", when: "Today, 2:14 pm", active: true },
  { id: "h2", title: "Plan for graduating early", when: "Yesterday" },
  { id: "h3", title: "Minor in Data Analytics requirements", when: "Mon" },
  { id: "h4", title: "How does CS 350 fit with 330?", when: "Apr 14" },
  { id: "h5", title: "Study abroad — summer options", when: "Apr 11" },
  { id: "h6", title: "Internship credit eligibility", when: "Apr 03" },
];

// Suggested starter prompts — derived from the student's own major/year/student
// type instead of one static list for everyone.
function getSuggestions(currentUser) {
  const isGrad = currentUser && currentUser.studentType === "graduate";
  const hasMajor = currentUser && currentUser.major && currentUser.major !== "Not set";
  const major = hasMajor ? currentUser.major.split(",")[0] : null;
  const year = currentUser && currentUser.year && currentUser.year !== "Not set" ? currentUser.year : null;

  const semestersLeft = { "Freshman": "eight", "Sophomore": "six", "Junior": "four", "Senior": "two" }[year];

  const planningQ = isGrad
    ? "Build a plan that gets me through the program on schedule."
    : semestersLeft
      ? `Build a ${semestersLeft}-semester plan that lets me graduate on time.`
      : "Build a semester-by-semester plan that lets me graduate on time.";

  const prereqQ = hasMajor
    ? `What prerequisites do I still need for my ${major} requirements?`
    : "What prerequisites do I still need for my required major courses?";

  const electivesQ = isGrad
    ? "Which elective or Career Leadership topic best fits my goals?"
    : "Which elective or concentration best fits my interests and schedule?";

  return [
    { label: "Planning",      q: planningQ },
    { label: "Prerequisites", q: prereqQ },
    { label: "Progress",      q: "Am I on track to graduate by my expected date?" },
    { label: "Electives",     q: electivesQ },
  ];
}

// Scripted demo reply — mimics a tool-calling assistant response
const DEMO_REPLY = {
  intro: "Good question — let me check your transcript against the CSCE 585 prerequisites.",
  tool: {
    name: "check_prerequisites",
    args: { course_code: "CSCE 585", student_id: 284511 },
    result: ["CSCE 145 ✓", "CSCE 146 ✓", "CSCE 240 ✓", "CSCE 350 — in progress", "CSCE 580 — not taken"],
  },
  body: [
    "**You're close, but not quite eligible yet.** CSCE 585 — Machine Learning Systems — requires CSCE 580 (Artificial Intelligence) as a prerequisite, which you haven't taken.",
    "Here's the cleanest path to take 585 in Spring 2027:",
    "**1.** Finish CSCE 350 this term (already enrolled — nice).\n**2.** Take CSCE 580 in Fall 2026 — it's only offered in the fall.\n**3.** Register for CSCE 585 in Spring 2027. That term it has 3 seats left as of today.",
    "If 580 fills up, I'd recommend joining the waitlist by October and emailing Prof. Hu — she typically opens 2–3 reserved seats for majors on track to take 585.",
  ],
  pills: [
    { code: "CSCE 580", title: "AI" },
    { code: "CSCE 350", title: "Data Structures" },
    { code: "CSCE 585", title: "ML Systems" },
  ],
};

// Real Darla Moore majors — must match frontend/onboarding.html's MAJORS list.
const MAJORS = [
  "Accounting, B.S.B.A.", "Business Economics, B.S.B.A.", "Economics, B.S.B.A.",
  "Finance, B.S.B.A.", "International Business, B.S.B.A.", "Management, B.S.B.A.",
  "Marketing, B.S.B.A.", "Operations and Supply Chain, B.S.B.A.",
  "Real Estate, B.S.B.A.", "Risk Management and Insurance, B.S.B.A."
];

// Graduate programs — must match frontend/onboarding.html's GRAD_PROGRAMS list.
const GRAD_PROGRAMS = [
  "Accountancy, M.A.C.C.", "Accountancy / Law, M.A.C.C. / J.D.", "Accounting and Audit Analytics, Certificate",
  "Artificial Intelligence in Business, Certificate",
  "Business Administration, M.S.", "Business Administration, One-Year M.B.A", "Business Administration, Ph.D.",
  "Business Administration, P.M.B.A.", "Business Analytics, M.S.", "Business Analytics, Certificate",
  "Economics, M.A.", "Economics, Ph.D.", "Economics / Law, M.A. / J.D.",
  "Human Resources, M.H.R.",
  "International Business, M.I.B.", "International Business, I.M.B.A.",
  "Operations and Supply Chain Management, Certificate",
  "Quantitative Economics, M.S.", "Retail Innovation, M.R.",
  "Sustainability in Business, Certificate"
];

Object.assign(window, {
  fetchCurrentUser, COURSES, COMPLETED, PLAN, REQUIREMENTS, HISTORY, getSuggestions, DEMO_REPLY, MAJORS, GRAD_PROGRAMS
});
