import {expect, test} from 'vitest'
import {type Connection, type Node} from '@xyflow/react'
import {
  atLeastOneEndNodeError,
  atLeastOneStartNodeError,
  checkAddConnection,
  checkAddNode,
  checkSave,
  hasCycleError,
  hasRedundantEdgeError,
  multiEndNodeError,
  multiStartNodeError,
  nodeToSelfError,
  sourceNodeNotFoundError,
  startNodeToEndNodeError,
  targetNodeNotFoundError
} from './FlowChecker.tsx'
import {uuid} from 'licia'

const createNode = (id: string, type: string): Node => {
  return {
    data: {},
    position: {
      x: 0,
      y: 0
    },
    id,
    type,
  }
}

const createStartNode = (id: string): Node => createNode(id, 'start-node')
const createEndNode = (id: string): Node => createNode(id, 'end-node')

const createConnection = function (source: string, target: string, sourceHandle: string | null = null, targetHandle: string | null = null): Connection {
  return {
    source,
    target,
    sourceHandle,
    targetHandle,
  }
}

/* check add node */

test(multiStartNodeError().message, () => {
  expect(() => checkAddNode('start-node', [createStartNode(uuid())], [])).toThrowError(multiStartNodeError())
})

test(multiEndNodeError().message, () => {
  expect(() => checkAddNode('end-node', [createEndNode(uuid())], [])).toThrowError(multiEndNodeError())
})

/* check add connection */
test(sourceNodeNotFoundError().message, () => {
  expect(() => checkAddConnection(createConnection('a', 'b'), [], []))
})

test(targetNodeNotFoundError().message, () => {
  expect(() => checkAddConnection(createConnection('a', 'b'), [createStartNode('a')], []))
})

test(startNodeToEndNodeError().message, () => {
  expect(() => checkAddConnection(
    createConnection('a', 'b'),
    [createStartNode('a'), createEndNode('b')],
    []
  ))
})

test(nodeToSelfError().message, () => {
  expect(() => {
    // language=JSON
    const {
      nodes,
      edges
    } = JSON.parse('{\n  "nodes": [\n    {\n      "id": "P14abHl4uY",\n      "type": "start-node",\n      "position": {\n        "x": 100,\n        "y": 100\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 82\n      }\n    },\n    {\n      "id": "3YDRebKqCX",\n      "type": "end-node",\n      "position": {\n        "x": 773.3027344262372,\n        "y": 101.42648884412338\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "YXJ91nHVaz",\n      "type": "llm-node",\n      "position": {\n        "x": 430.94541183662506,\n        "y": 101.42648884412338\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": true,\n      "dragging": false\n    }\n  ],\n  "edges": [\n    {\n      "source": "P14abHl4uY",\n      "target": "YXJ91nHVaz",\n      "id": "xy-edge__P14abHl4uY-YXJ91nHVaz"\n    },\n    {\n      "source": "YXJ91nHVaz",\n      "target": "3YDRebKqCX",\n      "id": "xy-edge__YXJ91nHVaz-3YDRebKqCX"\n    }\n  ],\n  "data": {}\n}')
    checkAddConnection(createConnection('YXJ91nHVaz', 'YXJ91nHVaz'), nodes, edges)
  }).toThrowError(nodeToSelfError())
})

test(hasCycleError().message, () => {
  expect(() => {
    // language=JSON
    const {
      nodes,
      edges,
    } = JSON.parse('{\n  "nodes": [\n    {\n      "id": "-DKfXm7r3f",\n      "type": "start-node",\n      "position": {\n        "x": -75.45812782717618,\n        "y": 14.410669352596976\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 82\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "2uL3Hw2CAW",\n      "type": "end-node",\n      "position": {\n        "x": 734.7875356349059,\n        "y": -1.2807079327602473\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "yp-yYfKUzC",\n      "type": "llm-node",\n      "position": {\n        "x": 338.2236369686051,\n        "y": -92.5759939566568\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "N4HQPN-NYZ",\n      "type": "llm-node",\n      "position": {\n        "x": 332.51768159211156,\n        "y": 114.26488844123382\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 74\n      },\n      "selected": true,\n      "dragging": false\n    }\n  ],\n  "edges": [\n    {\n      "source": "-DKfXm7r3f",\n      "target": "yp-yYfKUzC",\n      "id": "xy-edge__-DKfXm7r3f-yp-yYfKUzC"\n    },\n    {\n      "source": "yp-yYfKUzC",\n      "target": "2uL3Hw2CAW",\n      "id": "xy-edge__yp-yYfKUzC-2uL3Hw2CAW"\n    },\n    {\n      "source": "-DKfXm7r3f",\n      "target": "N4HQPN-NYZ",\n      "id": "xy-edge__-DKfXm7r3f-N4HQPN-NYZ"\n    },\n    {\n      "source": "N4HQPN-NYZ",\n      "target": "yp-yYfKUzC",\n      "id": "xy-edge__N4HQPN-NYZ-yp-yYfKUzC"\n    }\n  ],\n  "data": {}\n}')
    // language=JSON
    checkAddConnection(JSON.parse('{\n  "source": "yp-yYfKUzC",\n  "sourceHandle": null,\n  "target": "N4HQPN-NYZ",\n  "targetHandle": null\n}'), nodes, edges)
  }).toThrowError(hasCycleError())
})

test(hasRedundantEdgeError().message, () => {
  expect(() => {
    // language=JSON
    const {
      nodes,
      edges,
    } = JSON.parse('{\n  "nodes": [\n    {\n      "id": "ldoKAzHnKF",\n      "type": "llm-node",\n      "position": {\n        "x": 207,\n        "y": -38\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 105\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "1eJtMoJWs6",\n      "type": "llm-node",\n      "position": {\n        "x": 207,\n        "y": 172.5\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 105\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "7e5vQLDGTl",\n      "type": "start-node",\n      "position": {\n        "x": -162.3520537805597,\n        "y": 67.84901301708827\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 105\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "Wyqg_bXILg",\n      "type": "knowledge-node",\n      "position": {\n        "x": 560.402133595296,\n        "y": -38.892263766178665\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 75\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "7DaF-0G-yv",\n      "type": "llm-node",\n      "position": {\n        "x": 634.9924233956513,\n        "y": 172.01821084172227\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 75\n      },\n      "selected": false,\n      "dragging": false\n    },\n    {\n      "id": "mymIbw_W6k",\n      "type": "end-node",\n      "position": {\n        "x": 953.9302142661356,\n        "y": 172.0182108417223\n      },\n      "data": {},\n      "measured": {\n        "width": 256,\n        "height": 75\n      },\n      "selected": false,\n      "dragging": false\n    }\n  ],\n  "edges": [\n    {\n      "source": "7e5vQLDGTl",\n      "target": "ldoKAzHnKF",\n      "id": "xy-edge__7e5vQLDGTl-ldoKAzHnKF"\n    },\n    {\n      "source": "ldoKAzHnKF",\n      "target": "Wyqg_bXILg",\n      "id": "xy-edge__ldoKAzHnKF-Wyqg_bXILg"\n    },\n    {\n      "source": "7e5vQLDGTl",\n      "target": "1eJtMoJWs6",\n      "id": "xy-edge__7e5vQLDGTl-1eJtMoJWs6"\n    },\n    {\n      "source": "Wyqg_bXILg",\n      "target": "7DaF-0G-yv",\n      "id": "xy-edge__Wyqg_bXILg-7DaF-0G-yv"\n    },\n    {\n      "source": "1eJtMoJWs6",\n      "target": "7DaF-0G-yv",\n      "id": "xy-edge__1eJtMoJWs6-7DaF-0G-yv"\n    },\n    {\n      "source": "7DaF-0G-yv",\n      "target": "mymIbw_W6k",\n      "id": "xy-edge__7DaF-0G-yv-mymIbw_W6k"\n    }\n  ],\n  "data": {\n    "7e5vQLDGTl": {\n      "inputs": {\n        "question": {\n          "type": "text",\n          "description": "问题"\n        }\n      }\n    },\n    "ldoKAzHnKF": {\n      "model": "qwen3",\n      "outputs": {\n        "text": {\n          "type": "string"\n        }\n      },\n      "systemPrompt": "你是个聪明人"\n    },\n    "1eJtMoJWs6": {\n      "model": "deepseek",\n      "outputs": {\n        "text": {\n          "type": "string"\n        }\n      },\n      "systemPrompt": "你也是个好人"\n    }\n  }\n}')
    // language=JSON
    checkAddConnection(JSON.parse('{\n  "source": "1eJtMoJWs6",\n  "sourceHandle": null,\n  "target": "Wyqg_bXILg",\n  "targetHandle": null\n}'), nodes, edges)
  }).toThrowError(hasRedundantEdgeError())
})

/* check save */
test(atLeastOneStartNodeError().message, () => {
  expect(() => checkSave([], [], {})).toThrowError(atLeastOneStartNodeError())
})

test(atLeastOneEndNodeError().message, () => {
  expect(() => checkSave([createStartNode(uuid())], [], {})).toThrowError(atLeastOneEndNodeError())
})
