import { NextRequest, NextResponse } from 'next/server' import bcrypt from 'bcryptjs' import { getDatabase } from '../../../../lib/database/index' export async function POST(request: NextRequest) { try { const db = await getDatabase() const { email, password, firstName, lastName, phone } = await request.json() if (!email || !password || !firstName || !lastName) { return NextResponse.json( { error: 'Missing required fields' }, { status: 400 } ) } const existingUser = await db.getUserByEmail(email) if (existingUser) { return NextResponse.json( { error: 'User already exists' }, { status: 409 } ) } const hashedPassword = await bcrypt.hash(password, 10) const newUser = await db.createUser({ email, firstName, lastName, password: hashedPassword, phone, role: 'client' }) const newClient = await db.createClient({ userId: newUser.id, membershipType: 'basic', membershipStatus: 'active', joinDate: new Date() }) const { password: _, ...userWithoutPassword } = newUser return NextResponse.json( { message: 'User registered successfully', user: { ...userWithoutPassword, client: newClient } }, { status: 201 } ) } catch (error) { console.error('Registration error:', error) return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ) } } export async function GET() { try { const db = await getDatabase() const allUsers = await db.getAllUsers() const usersWithoutPassword = allUsers.map(({ password: _, ...user }) => user) return NextResponse.json({ users: usersWithoutPassword }) } catch (error) { console.error('Get users error:', error) return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ) } }