import type {NextRequest} from 'next/server';
import {ok, parseQuery, requireApiSession, simulate} from '@/shared/services/apiRoute';
import {readProfile} from '@/features/settings/mock/settings.mock';

export const dynamic = 'force-dynamic';

export async function GET(req: NextRequest) {
  const denied = await requireApiSession();
  if (denied) return denied;
  const q = parseQuery(req);
  const simulated = await simulate(q);
  if (simulated) return simulated;
  return ok(readProfile(), q);
}

/**
 * Accepts a profile patch and echoes the merged result.
 *
 * There is no persistence layer yet, so this deliberately does NOT pretend to
 * save — it validates the shape and returns what the merged record would be,
 * which is enough for the form to exercise its success path honestly.
 */
export async function PATCH(req: NextRequest) {
  const denied = await requireApiSession();
  if (denied) return denied;
  const q = parseQuery(req);
  const simulated = await simulate(q);
  if (simulated) return simulated;

  const patch = (await req.json()) as Record<string, unknown>;
  return ok({...readProfile(), ...patch}, q);
}
