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

// Cache fabric families for 15 minutes
export const revalidate = 900;
export const dynamic = 'force-dynamic'; // Mark as dynamic since serverApiClient uses cookies

/**
 * Get all fabric families
 * Cached server-side to reduce costs on AWS Amplify
 */
export async function GET() {
  try {
    const result = await serverApiClient('fabric-family', {
      method: 'GET',
      revalidate: 900, // Cache for 15 minutes
    });

    if (!result.success) {
      return NextResponse.json(
        {
          success: false,
          message: result.message || 'Failed to fetch fabric families',
          data: { fabric: [] }
        },
        { status: 500 }
      );
    }

    return NextResponse.json(
      {
        success: true,
        data: result.data,
      },
      {
        headers: {
          'Cache-Control': 'public, s-maxage=900, stale-while-revalidate=1800',
        },
      }
    );
  } catch (error: any) {
    console.error('Error fetching fabric families:', error);
    return NextResponse.json(
      {
        success: false,
        message: error.message || 'Failed to fetch fabric families',
        data: { fabric: [] }
      },
      { status: 500 }
    );
  }
}
