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

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 (
        <div className="container mx-auto p-6">
          <div className="bg-racing-red/10 border border-racing-red text-racing-red px-4 py-3 rounded-lg">
            Access denied - You must be logged in as an Owner or Admin
          </div>
        </div>
      );
    }
    return (
      <div className="container mx-auto p-6">
        <div className="bg-racing-red/10 border border-racing-red text-racing-red px-4 py-3 rounded-lg">
          Failed to load users: {error}
        </div>
      </div>
    );
  }

  const viewData = result.unwrap();

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