/* eslint-disable @typescript-eslint/no-explicit-any */
import { NextRequest, NextResponse } from 'next/server';
import { getIronSession } from 'iron-session';
import { sessionOptions, SessionData } from '../../../session';
import { cookies } from 'next/headers';

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { userName, firstName, lastName, occupation, gender } = body;

    const session = await getIronSession<SessionData>(await cookies(), sessionOptions);

    if (!session.signupData) {
      return NextResponse.json(
        { error: 'No signup data found. Please start from step 1.' },
        { status: 404 }
      );
    }

    const completeSignupData = {
      ...session.signupData,
      confirmPassword: session.signupData.password,
      userName,
      firstName,
      lastName,
      occupation,
      gender,
      step: 2,
      createdAt: new Date().toISOString()
    };

    // Return the complete data for the client to handle the API call
    return NextResponse.json({
      success: true,
      data: completeSignupData,
      email: session.signupData.email
    });

  } catch (error: any) {
    console.error('Complete signup error:', error);
    
    return NextResponse.json(
      { error: 'Internal server error' },
      { status: 500 }
    );
  }
}