import { NextResponse } from 'next/server';
import { db } from '@db/index';
import { usersMerged } from '@schemas/schema';
import bcrypt from 'bcryptjs';
import { eq } from 'drizzle-orm';

export async function POST(request: Request) {
  try {
    const { firstName, username, password, email } = await request.json();
    const hashedPassword = await bcrypt.hash(password, 10);

    const newUser = {
      firstName,
      username,
      email,
      passwordHash: hashedPassword,
    } satisfies typeof usersMerged.$inferInsert;

    await db.insert(usersMerged).values(newUser);

    return NextResponse.json(
      { message: 'User created successfully', redirect: '/' },
      { status: 201 }
    );
  } catch (error) {
    console.error('Signup error:', error);
    return NextResponse.json(
      { error: 'Failed to create user' },
      { status: 500 }
    );
  }
} 