import { AdminUsersPageQuery } from '@/lib/page-queries/AdminUsersPageQuery';
import { AdminUsersWrapper } from './AdminUsersWrapper';
import { ErrorBanner } from '@/components/ui/ErrorBanner';

interface AdminUsersPageProps {
  searchParams?: {
    search?: string;
    role?: string;
    status?: string;
    page?: string;
  };
}

export default async function AdminUsersPage({ searchParams }: AdminUsersPageProps) {
  // Parse query parameters
  const query = {
    search: searchParams?.search,
    role: searchParams?.role,
    status: searchParams?.status,
    page: searchParams?.page ? parseInt(searchParams.page, 10) : 1,
    limit: 50,
  };

  // Execute PageQuery using static method
  const result = await AdminUsersPageQuery.execute(query);

  // Handle errors
  if (result.isErr()) {
    const error = result.getError();
    if (error === 'notFound') {
      return (
        <ErrorBanner
          title="Access Denied"
          message="You must be logged in as an Owner or Admin"
          variant="error"
        />
      );
    }
    return (
      <ErrorBanner
        title="Load Failed"
        message={`Failed to load users: ${error}`}
        variant="error"
      />
    );
  }

  const output = result.unwrap();

  // Pass to client wrapper for UI interactions
  return <AdminUsersWrapper initialViewData={output} />;
}