import { describe, it, expect } from 'vitest';
import { createStore } from 'jotai';
import Graph from 'graphology';
import type { GraphNodeAttributes, GraphEdgeAttributes, DBGraphNode, DBGraphEdge } from '../core/types';
import { graphAtom, graphOptions, currentGraphIdAtom } from '../core/graph-store';
import { edgeFamilyAtom } from '../core/graph-derived';
import { addNodeToLocalGraphAtom, editingEdgeLabelAtom, updateEdgeLabelAtom } from '../core/graph-mutations';
import { addEdgeToLocalGraphAtom } from '../core/graph-mutations-edges';

// --- Helpers ---

function freshStore() {
	const store = createStore();
	store.set(graphAtom, new Graph<GraphNodeAttributes, GraphEdgeAttributes>(graphOptions));
	store.set(currentGraphIdAtom, 'test-graph');
	return store;
}

function makeDbNode(id: string, x = 0, y = 0): DBGraphNode {
	return {
		id,
		graph_id: 'test-graph',
		label: `Node ${id}`,
		node_type: 'default',
		configuration: null,
		ui_properties: { x, y, width: 200, height: 100, size: 15, zIndex: 0 },
		data: null,
		created_at: '2024-01-01T00:00:00Z',
		updated_at: '2024-01-01T00:00:00Z',
	};
}

function makeDbEdge(id: string, source: string, target: string): DBGraphEdge {
	return {
		id,
		graph_id: 'test-graph',
		source_node_id: source,
		target_node_id: target,
		edge_type: 'my-label',
		filter_condition: null,
		ui_properties: null,
		data: null,
		created_at: '2024-01-01T00:00:00Z',
		updated_at: '2024-01-01T00:00:00Z',
	};
}

// --- Tests ---

describe('edge-label-editing', () => {
	describe('editingEdgeLabelAtom', () => {
		it('starts as null', () => {
			const store = freshStore();
			expect(store.get(editingEdgeLabelAtom)).toBeNull();
		});

		it('can be set and cleared', () => {
			const store = freshStore();
			store.set(editingEdgeLabelAtom, 'edge-1');
			expect(store.get(editingEdgeLabelAtom)).toBe('edge-1');

			store.set(editingEdgeLabelAtom, null);
			expect(store.get(editingEdgeLabelAtom)).toBeNull();
		});
	});

	describe('updateEdgeLabelAtom', () => {
		it('updates the edge label in the graph', () => {
			const store = freshStore();
			store.set(addNodeToLocalGraphAtom, makeDbNode('a', 0, 0));
			store.set(addNodeToLocalGraphAtom, makeDbNode('b', 200, 200));
			store.set(addEdgeToLocalGraphAtom, makeDbEdge('e1', 'a', 'b'));

			// Initial label comes from edge_type
			const edgeBefore = store.get(edgeFamilyAtom('e1'));
			expect(edgeBefore!.label).toBe('my-label');

			// Update label
			store.set(updateEdgeLabelAtom, { edgeKey: 'e1', label: 'new-label' });

			const edgeAfter = store.get(edgeFamilyAtom('e1'));
			expect(edgeAfter!.label).toBe('new-label');
		});

		it('clears label when set to empty string', () => {
			const store = freshStore();
			store.set(addNodeToLocalGraphAtom, makeDbNode('a', 0, 0));
			store.set(addNodeToLocalGraphAtom, makeDbNode('b', 200, 200));
			store.set(addEdgeToLocalGraphAtom, makeDbEdge('e1', 'a', 'b'));

			store.set(updateEdgeLabelAtom, { edgeKey: 'e1', label: '' });

			const edge = store.get(edgeFamilyAtom('e1'));
			expect(edge!.label).toBeUndefined();
		});

		it('does nothing for non-existent edge', () => {
			const store = freshStore();
			// Should not throw
			store.set(updateEdgeLabelAtom, { edgeKey: 'nonexistent', label: 'test' });
		});
	});
});
