'use server';

import { AcceptSponsorshipRequestMutation } from '@/lib/mutations/sponsors/AcceptSponsorshipRequestMutation';
import { RejectSponsorshipRequestMutation } from '@/lib/mutations/sponsors/RejectSponsorshipRequestMutation';
import { SessionGateway } from '@/lib/gateways/SessionGateway';
import { revalidatePath } from 'next/cache';
import { Result } from '@/lib/contracts/Result';
import { routes } from '@/lib/routing/RouteConfig';

export async function acceptSponsorshipRequest(
  requestId: string,
): Promise<Result<void, string>> {
  // Get session for actorDriverId
  const sessionGateway = new SessionGateway();
  const session = await sessionGateway.getSession();
  const actorDriverId = session?.user?.primaryDriverId;
  
  if (!actorDriverId) {
    return Result.err('Not authenticated');
  }

  const mutation = new AcceptSponsorshipRequestMutation();
  const result = await mutation.execute({ requestId, actorDriverId });
  
  if (result.isErr()) {
    console.error('Failed to accept sponsorship request:', result.getError());
    return Result.err(result.getError());
  }
  
  revalidatePath(routes.protected.profileSponsorshipRequests);
  return Result.ok(undefined);
}

export async function rejectSponsorshipRequest(
  requestId: string,
  reason?: string,
): Promise<Result<void, string>> {
  // Get session for actorDriverId
  const sessionGateway = new SessionGateway();
  const session = await sessionGateway.getSession();
  const actorDriverId = session?.user?.primaryDriverId;
  
  if (!actorDriverId) {
    return Result.err('Not authenticated');
  }

  const mutation = new RejectSponsorshipRequestMutation();
  const result = await mutation.execute({ requestId, actorDriverId, reason: reason || null });
  
  if (result.isErr()) {
    console.error('Failed to reject sponsorship request:', result.getError());
    return Result.err(result.getError());
  }
  
  revalidatePath(routes.protected.profileSponsorshipRequests);
  return Result.ok(undefined);
}
