import { SponsorshipRequestsPageViewDataBuilder } from '@/lib/builders/view-data/SponsorshipRequestsPageViewDataBuilder';
import { AcceptSponsorshipRequestMutation } from '@/lib/mutations/sponsors/AcceptSponsorshipRequestMutation';
import { RejectSponsorshipRequestMutation } from '@/lib/mutations/sponsors/RejectSponsorshipRequestMutation';
import { SponsorshipRequestsPageQuery } from '@/lib/page-queries/page-queries/SponsorshipRequestsPageQuery';
import { SponsorshipRequestsClient } from '@/app/profile/sponsorship-requests/SponsorshipRequestsClient';

export interface SponsorshipRequestsTemplateProps {
  searchParams: Record<string, string>;
}

export async function SponsorshipRequestsTemplate({
  searchParams,
}: SponsorshipRequestsTemplateProps) {
  const pageQuery = new SponsorshipRequestsPageQuery();
  const viewDataBuilder = new SponsorshipRequestsPageViewDataBuilder();
  const acceptMutation = new AcceptSponsorshipRequestMutation();
  const rejectMutation = new RejectSponsorshipRequestMutation();

  const queryResult = await pageQuery.execute(searchParams);
  
  if (queryResult.isErr()) {
    // Handle error - redirect or show error page
    throw new Error('Failed to load sponsorship requests');
  }
  
  const viewData = viewDataBuilder.build(queryResult.unwrap());

  return (
    <SponsorshipRequestsClient
      viewData={viewData}
      acceptMutation={acceptMutation}
      rejectMutation={rejectMutation}
    />
  );
}
