import { INodeProperties } from 'n8n-workflow';

export const portGroupsOperations: INodeProperties[] = [
	{
		displayName: 'Operation',
		name: 'operation',
		type: 'options',
		noDataExpression: true,
		displayOptions: {
			show: {
				resource: ['portGroups'],
			},
		},
		options: [
			{
				name: 'Get All',
				value: 'getAll',
				description: 'Get all port groups',
				action: 'Get all port groups',
				routing: {
					request: {
						method: 'GET',
						url: '/port_groups',
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'groups',
								},
							},
						],
					},
				},
			},
			{
				name: 'Get Ports by Group',
				value: 'getPortsByGroup',
				description: 'Get all ports in a group',
				action: 'Get ports by group',
				routing: {
					request: {
						method: 'GET',
						url: '=/port_groups/{{encodeURIComponent($parameter.groupName)}}',
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'ports',
								},
							},
						],
					},
				},
			},
			{
				name: 'Create',
				value: 'create',
				description: 'Create a new port group',
				action: 'Create port group',
				routing: {
					request: {
						method: 'POST',
						url: '/port_groups',
					},
					send: {
						type: 'body',
					},
				},
			},
			{
				name: 'Assign Ports',
				value: 'assignPorts',
				description: 'Assign ports to a group',
				action: 'Assign ports to group',
				routing: {
					request: {
						method: 'POST',
						url: '=/port_groups/{{$parameter.portGroupId}}/assign',
					},
					send: {
						type: 'body',
					},
				},
			},
			{
				name: 'Remove Ports',
				value: 'removePorts',
				description: 'Remove ports from a group',
				action: 'Remove ports from group',
				routing: {
					request: {
						method: 'POST',
						url: '=/port_groups/{{$parameter.portGroupId}}/remove',
					},
					send: {
						type: 'body',
					},
				},
			},
			{
				name: 'Get Graph',
				value: 'getGraph',
				description: 'Get port group graph',
				action: 'Get port group graph',
				routing: {
					request: {
						method: 'GET',
						url: '=/portgroups/{{$parameter.groupTypes}}',
					},
				},
			},
			{
				name: 'Get Multi-Port Graph',
				value: 'getMultiPortGraph',
				description: 'Get graph for multiple ports',
				action: 'Get multi-port graph',
				routing: {
					request: {
						method: 'GET',
						url: '=/portgroups/multiport/bits/{{$parameter.portIds}}',
					},
				},
			},
		],
		default: 'getAll',
	},
];

export const portGroupsFields: INodeProperties[] = [
	// Group name for operations that need it
	{
		displayName: 'Group Name',
		name: 'groupName',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['getPortsByGroup'],
			},
		},
		default: '',
		description: 'Port group name',
	},

	// Port Group ID for assignment operations
	{
		displayName: 'Port Group ID',
		name: 'portGroupId',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['assignPorts', 'removePorts'],
			},
		},
		default: '',
		description: 'Port group ID',
	},

	// Fields for creating port groups
	{
		displayName: 'Group Name',
		name: 'name',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['create'],
			},
		},
		default: '',
		description: 'Name of the port group',
	},
	{
		displayName: 'Description',
		name: 'desc',
		type: 'string',
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['create'],
			},
		},
		default: '',
		description: 'Description of the port group',
	},

	// Port management
	{
		displayName: 'Port IDs',
		name: 'port_ids',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['assignPorts', 'removePorts'],
			},
		},
		default: '',
		placeholder: '1,2,3',
		description: 'Comma-separated list of port IDs',
	},

	// Port IDs for multi-port graph
	{
		displayName: 'Port IDs',
		name: 'portIds',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['getMultiPortGraph'],
			},
		},
		default: '',
		placeholder: '1,2,3',
		description: 'Comma-separated list of port IDs for graph',
	},

	// Group types for graph
	{
		displayName: 'Group Types',
		name: 'groupTypes',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['getGraph'],
			},
		},
		default: '',
		placeholder: 'transit,peering',
		description: 'Comma-separated list of group types (e.g., transit, peering)',
	},

	// Options for getPortsByGroup
	{
		displayName: 'Options',
		name: 'options',
		type: 'collection',
		placeholder: 'Add Option',
		default: {},
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['getPortsByGroup'],
			},
		},
		options: [
			{
				displayName: 'Full Data',
				name: 'full',
				type: 'boolean',
				default: false,
				description: 'Return all port data instead of just IDs',
				routing: {
					send: {
						type: 'query',
						property: 'full',
						value: '={{$value ? "1" : ""}}',
					},
				},
			},
		],
	},

	// Graph options
	{
		displayName: 'Graph Options',
		name: 'graphOptions',
		type: 'collection',
		placeholder: 'Add Option',
		default: {},
		displayOptions: {
			show: {
				resource: ['portGroups'],
				operation: ['getGraph', 'getMultiPortGraph'],
			},
		},
		options: [
			{
				displayName: 'From',
				name: 'from',
				type: 'string',
				default: '',
				description: 'Start time for graph data',
				routing: {
					send: {
						type: 'query',
						property: 'from',
					},
				},
			},
			{
				displayName: 'To',
				name: 'to',
				type: 'string',
				default: '',
				description: 'End time for graph data',
				routing: {
					send: {
						type: 'query',
						property: 'to',
					},
				},
			},
			{
				displayName: 'Width',
				name: 'width',
				type: 'number',
				default: 1075,
				description: 'Graph width in pixels',
				routing: {
					send: {
						type: 'query',
						property: 'width',
					},
				},
			},
			{
				displayName: 'Height',
				name: 'height',
				type: 'number',
				default: 300,
				description: 'Graph height in pixels',
				routing: {
					send: {
						type: 'query',
						property: 'height',
					},
				},
			},
		],
	},
];
