"use client";

import { Loader } from "lucide-react";

import { Card, CardContent } from "@/components/ui/card";
import { useGetMembers } from "@/features/members/api/use-get-members";
import { useGetCurrentMember } from "@/features/members/api/use-get-current-member";
import { useGetProjects } from "@/features/projects/api/use-get-projects";
import { useWorkspaceId } from "@/features/workspaces/hooks/use-workspace-id";
import { useGetEpic } from "../api/use-get-epic";

import { EditEpicForm } from "./edit-epic-form";

interface EditEpicFormWrapperProps {
  onCancel: () => void;
  id: string;
}

export const EditEpicFormWrapper = ({
  onCancel,
  id,
}: EditEpicFormWrapperProps) => {
  const workspaceId = useWorkspaceId();

  const { data: epic, isLoading: isLoadingEpic } = useGetEpic({
    epicId: id,
  });

  const { data: projects, isLoading: isLoadingProjects } = useGetProjects({
    workspaceId,
  });
  const { data: members, isLoading: isLoadingMembers } = useGetMembers({
    workspaceId,
  });
  const { data: currentMember, isLoading: isLoadingCurrentMember } = useGetCurrentMember({
    workspaceId,
  });

  const projectOptions = projects?.documents ?? [];
  const memberOptions = members?.documents ?? [];

  const isLoading = isLoadingEpic || isLoadingProjects || isLoadingMembers || isLoadingCurrentMember;

  if (isLoading) {
    return (
      <Card className="size-full border-none shadow-none">
        <CardContent className="flex items-center justify-center h-[200px]">
          <Loader className="size-5 animate-spin text-muted-foreground" />
        </CardContent>
      </Card>
    );
  }

  if (!epic) {
    return (
      <Card className="size-full border-none shadow-none">
        <CardContent className="flex items-center justify-center h-[200px]">
          <p className="text-muted-foreground">Epic not found</p>
        </CardContent>
      </Card>
    );
  }

  return (
    <EditEpicForm
      onCancel={onCancel}
      projectOptions={projectOptions}
      memberOptions={memberOptions}
      initialValues={epic as any}
      currentMember={currentMember}
    />
  );
}; 