import { NextRequest, NextResponse } from "next/server";

interface RegisterRequest {
  firstName: string;
  lastName: string;
  email: string;
  password: string;
  userType: "student" | "teacher";
}

// This is a mock implementation. In production, you would:
// 1. Hash the password using bcrypt or similar
// 2. Store the user in a database
// 3. Send verification email
// 4. Create JWT tokens

const mockUsers: { [key: string]: RegisterRequest & { id: string } } = {};

export async function POST(request: NextRequest) {
  try {
    const body: RegisterRequest = await request.json();

    // Validate input
    if (
      !body.firstName ||
      !body.lastName ||
      !body.email ||
      !body.password ||
      !body.userType
    ) {
      return NextResponse.json(
        { message: "Tüm alanlar zorunludur" },
        { status: 400 }
      );
    }

    // Check if user already exists
    if (mockUsers[body.email]) {
      return NextResponse.json(
        { message: "Bu e-posta adresi zaten kullanımda" },
        { status: 409 }
      );
    }

    // In production, hash the password
    // const hashedPassword = await bcrypt.hash(body.password, 10);

    // Create new user
    const newUser = {
      id: `user_${Date.now()}`,
      firstName: body.firstName,
      lastName: body.lastName,
      email: body.email,
      password: body.password, // Never store plaintext in production!
      userType: body.userType,
    };

    mockUsers[body.email] = newUser;

    // In production, you would:
    // 1. Create a JWT token
    // 2. Set secure HTTP-only cookie
    // 3. Send verification email

    return NextResponse.json(
      {
        message: "Kayıt başarıyla tamamlandı",        user: {
          id: newUser.id,
          firstName: newUser.firstName,
          lastName: newUser.lastName,
          email: newUser.email,
          userType: newUser.userType,
        },
      },
      { status: 201 }
    );
  } catch (error) {
    console.error("Registration error:", error);
    return NextResponse.json(
      { message: "Sunucu hatası oluştu" },
      { status: 500 }
    );
  }
}
