import { Request, AsyncRequest } from '@/utils/request'
import { getRequestId } from '@/utils/misc'
import { serverRequest, type RequestBody, type StandardResponse } from '@/api/AgentApi'

const URL_GET_NODE = '/api/kb/nodes'
const URL_GET_EDGE = '/api/kb/edges'
const URL_GET_SUMMARY = '/api/kb/summary'
const URL_GET_SCHEMA = '/api/kb/schemas'
const URL_GET_GRAPHS = '/api/kb/graphs'
const URL_GET_STD_SCHEMA = '/api/kb/std-schemas'
const URL_GET_REPORT = '/api/kb/report'

export const GraphStatus = {
    GRAPH_STATUS_NORMAL: 0,
    GRAPH_STATUS_DELETED: -1,
    GRAPH_STATUS_NEED_CHECK: 1,
    GRAPH_STATUS_CHECKING: 2,
    GRAPH_STATUS_CHECKED: 3,
}
export function getGraphCheckReport(graph_id: number): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_latest_report',
        params: [
            { name: 'graph_id', value: graph_id },
        ]
    }
    return serverRequest(URL_GET_REPORT, data)
}
export function getGraphCheckReportEvents(graph_id: number, page: number, page_size: number, event_name: string): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_report_events',
        params: [
            { name: 'graph_id', value: graph_id },
            { name: 'page', value: page },
            { name: 'page_size', value: page_size },
            { name: 'event_name', value: event_name },
        ]
    }
    return serverRequest(URL_GET_REPORT, data)
}
export function getGraphCheckProgressValue(progressData: any): number {
    for (var i = 0; i < progressData.length; i++) {
        if (progressData[i].name == "progress") {
            return Math.ceil(progressData[i].value * 1000) / 10
        }
    }
    return 0
}

export function getGraphCheckProgressData(progressData: any): any {
    for (var i = 0; i < progressData.length; i++) {
        if (progressData[i].name == "progress") {
            return progressData[i].data
        }
    }
    return 0
}

export function getGraph(graph_id: number): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_graph',
        params: [
            { name: 'graph_id', value: graph_id },
        ]
    }
    return serverRequest(URL_GET_GRAPHS, data)
}
export function getGraphs(params: any): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_graphs',
        params: [
            { name: 'page', value: params['page'] },
            { name: 'page_size', value: params['page_size'] },
        ]
    }
    return serverRequest(URL_GET_GRAPHS, data)
}

export function searchNodes(keyword: string, graph_id: number, category: string = "any"): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'search_nodes',
        params: [
            { name: 'name', value: keyword },
            { name: 'category', value: category },
            { name: 'graph_id', value: graph_id },
        ]
    }
    return serverRequest(URL_GET_NODE, data)
}

export function getNode(node_id: number, graph_id: number): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_node',
        params: [
            { name: 'node_id', value: node_id },
            { name: 'graph_id', value: graph_id },
        ]
    }
    return serverRequest(URL_GET_NODE, data)
}

// export function getIndividualNodes(graph_id:number): Promise<StandardResponse> {
//     var data:RequestBody = {
//         id: getRequestId(),
//         action:'get_individual_nodes',
//         params:[
//             {name: 'graph_id', value: graph_id},
//         ]
//     } 
//     return serverRequest(URL_GET_NODE, data)
// }

// export function getContradictions(graph_id:number): Promise<StandardResponse> {
//     var data:RequestBody = {
//         id: getRequestId(),
//         action:'get_contradictions',
//         params:[
//             {name: 'graph_id', value: graph_id},
//         ]
//     } 
//     return serverRequest(URL_GET_NODE, data)
// }
export function updateNode(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'update_node',
        params: [
            { name: 'node_id', value: params['node_id'] },
            { name: 'graph_id', value: params['graph_id'] },
            { name: 'name', value: params['name'] },
            { name: 'category', value: params['category'] },
        ]
    }
    return serverRequest(URL_GET_NODE, data)
}
export function searchEdges(keyword: string, graph_id: number, category: string = "any"): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'search_edges',
        params: [
            { name: 'name', value: keyword },
            { name: 'category', value: category },
            { name: 'graph_id', value: graph_id },
        ]
    }
    return serverRequest(URL_GET_EDGE, data)
}
export function getNodeNeighbors(node_id: number, graph_id: number): Promise<StandardResponse> {
    var data: RequestBody = {
        id: getRequestId(),
        action: 'neighbors',
        params: [
            { name: 'node_id', value: node_id },
            { name: 'graph_id', value: graph_id },
        ]
    }
    return serverRequest(URL_GET_NODE, data)
}
export function getGraphSummary(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_summary',
        params: [
            { name: 'graph_id', value: params['graph_id'] },
        ]
    }
    return serverRequest(URL_GET_SUMMARY, data)
}

export function getGraphNodeSchema(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_nodes_schemas',
        params: [
            { name: 'graph_id', value: params['graph_id'] },
        ]
    }
    return serverRequest(URL_GET_SCHEMA, data)
}


export function getGraphEdgeSchema(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_edges_schemas',
        params: [
            { name: 'graph_id', value: params['graph_id'] },
        ]
    }
    return serverRequest(URL_GET_SCHEMA, data)
}

export function getGraphStdSchemas(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'get_std_schemas',
        params: [
            { name: 'page', value: params['page'] },
            { name: 'page_size', value: params['page_size'] },
        ]
    }
    return serverRequest(URL_GET_STD_SCHEMA, data)
}

export function reindexSchema(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'index_std_schema',
        params: [
            { name: 'schema_id', value: params['schema_id'] },
        ]
    }
    return serverRequest(URL_GET_STD_SCHEMA, data)
}

export function searchStdSchema(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'std_schema_search',
        params: [
            { name: 'query_word', value: params['query_word'] },
            { name: 'type', value: params['type'] },
            { name: 'schema_id', value: params['schema_id'] },
        ]
    }
    return serverRequest(URL_GET_STD_SCHEMA, data)
}
export function updateStdSchemaContent(params: any): Promise<StandardResponse> {
    console.log("updateStdSchemaContent", params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'std_schema_update_content',
        params: [
            { name: 'schema_id', value: params['schema_id'] },
            { name: 'content', value: params['content'] },
        ]
    }
    return serverRequest(URL_GET_STD_SCHEMA, data)
}
export function updateGraphNodeSchema(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'update_node_schema',
        params: [
            { name: 'graph_id', value: params['graph_id'] },
            { name: 'category', value: params['category'] },
            { name: 'category_new', value: params['category_new'] },
        ]
    }
    return serverRequest(URL_GET_SCHEMA, data)
}
export function updateGraphEdgeSchema(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'update_edge_schema',
        params: [
            { name: 'graph_id', value: params['graph_id'] },
            { name: 'category', value: params['category'] },
            { name: 'category_new', value: params['category_new'] },
        ]
    }
    return serverRequest(URL_GET_SCHEMA, data)
}

export function createGraphCheckReport(params: any): Promise<StandardResponse> {
    console.log(params)
    var data: RequestBody = {
        id: getRequestId(),
        action: 'create_graph_check_report',
        params: [
            { name: 'graph_id', value: params['graph_id'] },
        ]
    }
    return serverRequest(URL_GET_REPORT, data)
}
export function getGraphCheckProgress(graph_id: number): Promise<StandardResponse> {

    var data: RequestBody = {
        id: getRequestId(),
        action: 'graph_report_progress',
        params: [
            { name: 'graph_id', value: graph_id },
        ]
    }
    return serverRequest(URL_GET_REPORT, data)
}