"use client";

import { useGetWorkspace } from "@/features/workspaces/api/use-get-workspace";
import { useGetCurrentMember } from "@/features/members/api/use-get-current-member";
import { useWorkspaceId } from "@/features/workspaces/hooks/use-workspace-id";
import { EditWorkspaceForm } from "@/features/workspaces/components/edit-workspace-form";
import { MemberRole } from "@/features/members/types";

import { PageError } from "@/components/page-error";
import { PageLoader } from "@/components/page-loader";

export const WorkspaceIdSettingsClient = () => {
  const workspaceId = useWorkspaceId();
  const { data: initialValues, isLoading: isLoadingWorkspace } = useGetWorkspace({ workspaceId });
  const { data: currentMember, isLoading: isLoadingMember } = useGetCurrentMember({ workspaceId });

  if (isLoadingWorkspace || isLoadingMember) {
    return <PageLoader />
  }

  if (!initialValues) {
    return <PageError message="Workspace not found" />
  }

  // Check if current user is admin
  if (currentMember?.role !== MemberRole.ADMIN) {
    return <PageError message="You don't have permission to access workspace settings" />
  }

  return (
    <div className="w-full lg:max-w-xl">
      <EditWorkspaceForm initialValues={initialValues} />
    </div>
  );
};
