import { NextResponse } from 'next/server';
import { serverApiClient } from '@/utils/serverApiClient';

// Always fetch fresh data - no caching
export const dynamic = 'force-dynamic';
export const revalidate = 0; // No caching

/**
 * Get user profile (server-side without caching)
 * Automatically includes auth token from cookies
 * Always fetches fresh data to reflect recent updates
 */
export async function GET() {
  try {
    const result = await serverApiClient('get-profile', {
      method: 'GET',
      cache: 'no-store', // Always fetch fresh data
    });

    if (!result.success) {
      return NextResponse.json(
        {
          success: false,
          message: result.message || 'Failed to fetch profile',
        },
        { status: 401 }
      );
    }

    return NextResponse.json(
      {
        success: true,
        stausCode: 200,
        data: result.data,
      },
      {
        headers: {
          'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate',
          'Pragma': 'no-cache',
          'Expires': '0',
        },
      }
    );
  } catch (error: any) {
    console.error('Error fetching user profile:', error);
    return NextResponse.json(
      {
        success: false,
        message: error.message || 'Failed to fetch profile',
      },
      { status: 500 }
    );
  }
}
