import { NextRequest, NextResponse } from 'next/server' import { supabase } from '@/lib/supabase' import { userService } from '@/lib/database' export async function PUT(request: NextRequest) { try { // Get the authorization header const authHeader = request.headers.get('authorization') if (!authHeader || !authHeader.startsWith('Bearer ')) { return NextResponse.json( { error: 'Authorization header required' }, { status: 401 } ) } const token = authHeader.substring(7) // Remove 'Bearer ' prefix // Verify the token and get user const { data: { user }, error: authError } = await supabase.auth.getUser(token) if (authError || !user) { console.error('Auth error:', authError) return NextResponse.json( { error: 'Invalid or expired token' }, { status: 401 } ) } const body = await request.json() const { username, user_type } = body // Validate input if (!username) { return NextResponse.json( { error: 'Username is required' }, { status: 400 } ) } // Validate user_type if provided if (user_type && !['admin', 'user', 'moderator'].includes(user_type)) { return NextResponse.json( { error: 'Invalid user type' }, { status: 400 } ) } // Get current user profile to get user_id const currentProfile = await userService.getByEmail(user.email!) if (!currentProfile) { return NextResponse.json( { error: 'User profile not found' }, { status: 404 } ) } // Only admins can change user_type; normal users cannot escalate to admin const allowedUserType = currentProfile.user_type === 'admin' && user_type ? user_type : currentProfile.user_type // Update user profile const updatedProfile = await userService.update(currentProfile.user_id, { user_name: username, user_type: allowedUserType }) return NextResponse.json({ success: true, message: 'Profile updated successfully', user: { id: user.id, email: user.email, username: updatedProfile.user_name, user_type: updatedProfile.user_type, created_at: user.created_at, last_sign_in: user.last_sign_in_at } }, { status: 200 }) } catch (error) { console.error('Update profile error:', error) return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ) } } export async function GET(request: NextRequest) { try { // Get the authorization header const authHeader = request.headers.get('authorization') if (!authHeader || !authHeader.startsWith('Bearer ')) { return NextResponse.json( { error: 'Authorization header required' }, { status: 401 } ) } const token = authHeader.substring(7) // Remove 'Bearer ' prefix // Verify the token and get user const { data: { user }, error: authError } = await supabase.auth.getUser(token) if (authError || !user) { console.error('Auth error:', authError) return NextResponse.json( { error: 'Invalid or expired token' }, { status: 401 } ) } // Get user profile from database const userProfile = await userService.getByEmail(user.email!) return NextResponse.json({ success: true, user: { id: user.id, email: user.email, username: userProfile.user_name, user_type: userProfile.user_type, created_at: user.created_at, last_sign_in: user.last_sign_in_at, email_confirmed: user.email_confirmed_at !== null } }, { status: 200 }) } catch (error) { console.error('Get profile error:', error) return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ) } }