import { type LoaderFunctionArgs, type ActionFunctionArgs } from '@remix-run/node';
import { API_BASE_URL } from '~/config/api-config';

/**
 * GET /api/dataset/datasets/:datasetId - 获取知识库详情
 * Dify API: GET /datasets/{dataset_id}
 */
export async function loader({ request, params }: LoaderFunctionArgs) {
    try {
        const { getUserSession } = await import("~/api/login/auth.server");
        const { frontendJWT } = await getUserSession(request);

        if (!frontendJWT) {
            return new Response(
                JSON.stringify({ error: 'JWT认证失败，请重新登录' }),
                { status: 401, headers: { 'Content-Type': 'application/json' } }
            );
        }

        const { datasetId } = params;
        if (!datasetId) {
            return new Response(
                JSON.stringify({ error: '缺少 datasetId 参数' }),
                { status: 400, headers: { 'Content-Type': 'application/json' } }
            );
        }

        // console.log('[API] Dataset Detail:', { datasetId });

        // 转发请求到 FastAPI -> Dify API
        const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}`;
        const response = await fetch(apiUrl, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${frontendJWT}`,
            },
        });

        const data = await response.json();

        return new Response(JSON.stringify(data), {
            status: response.status,
            headers: { 'Content-Type': 'application/json' },
        });

    } catch (error: any) {
        console.error('[API] Dataset Detail - Error:', error.message);
        return new Response(
            JSON.stringify({ error: error.message || 'Failed to get dataset' }),
            { status: 500, headers: { 'Content-Type': 'application/json' } }
        );
    }
}

/**
 * PATCH /api/dataset/datasets/:datasetId - 修改知识库设置
 *
 * Dify API: PATCH /datasets/{dataset_id}
 *
 * 请求体支持以下字段:
 * - name (string): 知识库名称（必填）
 * - retrieval_model (object): 检索模型配置（选填）
 *   - search_method: 'keyword_search' | 'semantic_search' | 'full_text_search' | 'hybrid_search'
 *   - reranking_enable: boolean
 *   - reranking_model: { reranking_provider_name, reranking_model_name }
 *   - weights: number | null (混合检索的语义权重)
 *   - top_k: number
 *   - score_threshold_enabled: boolean
 *   - score_threshold: number | null
 *
 * 注意：删除知识库功能不对外开放
 */
export async function action({ request, params }: ActionFunctionArgs) {
    try {
        const { getUserSession } = await import("~/api/login/auth.server");
        const { frontendJWT } = await getUserSession(request);

        if (!frontendJWT) {
            return new Response(
                JSON.stringify({ error: 'JWT认证失败，请重新登录' }),
                { status: 401, headers: { 'Content-Type': 'application/json' } }
            );
        }

        const { datasetId } = params;
        if (!datasetId) {
            return new Response(
                JSON.stringify({ error: '缺少 datasetId 参数' }),
                { status: 400, headers: { 'Content-Type': 'application/json' } }
            );
        }

        const method = request.method;

        if (method === 'PATCH') {
            const body = await request.json();

            // console.log('[API] body:', body);

            // name 是必填字段
            if (!body.name || typeof body.name !== 'string') {
                return new Response(
                    JSON.stringify({ error: '请提供有效的知识库名称 (name)' }),
                    { status: 400, headers: { 'Content-Type': 'application/json' } }
                );
            }

            const trimmedName = body.name.trim();
            if (trimmedName.length === 0) {
                return new Response(
                    JSON.stringify({ error: '知识库名称不能为空' }),
                    { status: 400, headers: { 'Content-Type': 'application/json' } }
                );
            }

            // 构建允许的请求体
            const allowedBody: Record<string, any> = {
                name: trimmedName,
            };

            // 可选: retrieval_model 检索模型配置
            if (body.retrieval_model && typeof body.retrieval_model === 'object') {
                const rm = body.retrieval_model;

                const name = body.name || '';

                // 验证 search_method
                const validSearchMethods = ['keyword_search', 'semantic_search', 'full_text_search', 'hybrid_search'];
                if (rm.search_method && !validSearchMethods.includes(rm.search_method)) {
                    return new Response(
                        JSON.stringify({ error: '无效的检索方法 (search_method)' }),
                        { status: 400, headers: { 'Content-Type': 'application/json' } }
                    );
                }

                allowedBody.name = name;

                allowedBody.retrieval_model = {
                    search_method: rm.search_method,
                    reranking_enable: rm.reranking_enable ?? false,
                    reranking_mode: rm.reranking_mode ?? null,
                    reranking_model: rm.reranking_model ?? {
                        reranking_provider_name: '',
                        reranking_model_name: '',
                    },
                    weights: rm.weights ?? null,
                    top_k: rm.top_k ?? 3,
                    score_threshold_enabled: rm.score_threshold_enabled ?? false,
                    score_threshold: rm.score_threshold_enabled ? (rm.score_threshold ?? null) : null,
                };
            }

            // console.log('[API] Update Dataset Settings:', { datasetId, body: allowedBody });

            const apiUrl = `${API_BASE_URL}/dify_dataset/datasets/${datasetId}`;
            const response = await fetch(apiUrl, {
                method: 'PATCH',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${frontendJWT}`,
                },
                body: JSON.stringify(allowedBody),
            });

            const data = await response.json();
            return new Response(JSON.stringify(data), {
                status: response.status,
                headers: { 'Content-Type': 'application/json' },
            });
        }

        return new Response(
            JSON.stringify({ error: 'Method not allowed' }),
            { status: 405, headers: { 'Content-Type': 'application/json' } }
        );

    } catch (error: any) {
        console.error('[API] Dataset Action - Error:', error.message);
        return new Response(
            JSON.stringify({ error: error.message || 'Failed to process dataset request' }),
            { status: 500, headers: { 'Content-Type': 'application/json' } }
        );
    }
}
