import { useEffect, useState } from "react";
import { Gantt } from "@svar-ui/react-gantt";
import "@svar-ui/react-gantt/style.css";

import type { GanttLinkDto, GanttTaskDto } from "@mrp/shared";

import { useAuth } from "../../auth/AuthProvider";
import { api } from "../../lib/api";
import { useTheme } from "../../theme/ThemeProvider";

export function GanttPage() {
  const { token } = useAuth();
  const { mode } = useTheme();
  const [tasks, setTasks] = useState<GanttTaskDto[]>([]);
  const [links, setLinks] = useState<GanttLinkDto[]>([]);

  useEffect(() => {
    if (!token) {
      return;
    }

    api.getGanttDemo(token).then((data) => {
      setTasks(data.tasks);
      setLinks(data.links);
    });
  }, [token]);

  return (
    <section className="rounded-[28px] border border-line/70 bg-surface/90 p-8 shadow-panel">
      <p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">Planning</p>
      <h3 className="mt-3 text-2xl font-bold text-text">SVAR Gantt Preview</h3>
      <p className="mt-2 text-sm text-muted">Theme-aware integration wrapper prepared for future manufacturing schedules and task dependencies.</p>
      <div
        className={`gantt-theme mt-6 overflow-hidden rounded-2xl border border-line/70 bg-page/70 p-4 ${
          mode === "dark" ? "wx-willow-dark-theme" : "wx-willow-theme"
        }`}
      >
        <Gantt
          tasks={tasks.map((task) => ({
            ...task,
            start: new Date(task.start),
            end: new Date(task.end),
          }))}
          links={links}
        />
      </div>
    </section>
  );
}
