import { json, type LoaderFunctionArgs } from '@remix-run/node';
import { difyClient } from '../services/dify-client.server';
import { getSessionInfo, commitSession } from '../utils/session.server';

export async function loader({ request }: LoaderFunctionArgs) {
    try {
        const { user, session } = await getSessionInfo(request);

        // ('📋 Parameters API - User:', user);

        const data = await difyClient.getApplicationParameters(user);

        // ('✅ Parameters API - Success:', data);

        return json(data, {
            headers: {
                'Set-Cookie': await commitSession(session),
            },
        });
    } catch (error: any) {
        console.error('❌ Parameters API - Error:', error);
        return json(
            { error: error.message || 'Failed to fetch parameters' },
            {
                status: 500,
                headers: {
                    'Set-Cookie': await commitSession((await getSessionInfo(request)).session),
                },
            }
        );
    }
} 