import { INodeProperties } from 'n8n-workflow';

export const locationsOperations: INodeProperties[] = [
	{
		displayName: 'Operation',
		name: 'operation',
		type: 'options',
		noDataExpression: true,
		displayOptions: {
			show: {
				resource: ['locations'],
			},
		},
		options: [
			{
				name: 'Get All',
				value: 'getAll',
				description: 'Get all locations',
				action: 'Get all locations',
				routing: {
					request: {
						method: 'GET',
						url: '/resources/locations',
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'locations',
								},
							},
						],
					},
				},
			},
			{
				name: 'Get',
				value: 'get',
				description: 'Get a specific location',
				action: 'Get a location',
				routing: {
					request: {
						method: 'GET',
						url: '=/location/{{$parameter.locationId}}',
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'get_location',
								},
							},
						],
					},
				},
			},
			{
				name: 'Create',
				value: 'create',
				description: 'Add a new location',
				action: 'Create a location',
				routing: {
					request: {
						method: 'POST',
						url: '/locations',
					},
					send: {
						type: 'body',
					},
				},
			},
			{
				name: 'Update',
				value: 'update',
				description: 'Update a location',
				action: 'Update a location',
				routing: {
					request: {
						method: 'PATCH',
						url: '=/locations/{{$parameter.locationId}}',
					},
					send: {
						type: 'body',
					},
				},
			},
			{
				name: 'Delete',
				value: 'delete',
				description: 'Delete a location',
				action: 'Delete a location',
				routing: {
					request: {
						method: 'DELETE',
						url: '=/locations/{{$parameter.locationId}}',
					},
				},
			},
		],
		default: 'getAll',
	},
];

export const locationsFields: INodeProperties[] = [
	// Location ID for specific operations
	{
		displayName: 'Location',
		name: 'locationId',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['locations'],
				operation: ['get', 'update', 'delete'],
			},
		},
		default: '',
		placeholder: 'location name or ID',
		description: 'Location name or ID',
	},

	// Fields for creating locations
	{
		displayName: 'Location Name',
		name: 'location',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['locations'],
				operation: ['create'],
			},
		},
		default: '',
		description: 'Name of the location',
	},

	// Location coordinates
	{
		displayName: 'Coordinates',
		name: 'coordinates',
		type: 'collection',
		placeholder: 'Add Coordinates',
		default: {},
		displayOptions: {
			show: {
				resource: ['locations'],
				operation: ['create', 'update'],
			},
		},
		options: [
			{
				displayName: 'Latitude',
				name: 'lat',
				type: 'string',
				default: '',
				placeholder: '37.4220041',
				description: 'Latitude coordinate',
			},
			{
				displayName: 'Longitude',
				name: 'lng',
				type: 'string',
				default: '',
				placeholder: '-122.0862462',
				description: 'Longitude coordinate',
			},
			{
				displayName: 'Fixed Coordinates',
				name: 'fixed_coordinates',
				type: 'boolean',
				default: true,
				description: 'Whether coordinates are fixed or can be updated from device',
			},
		],
	},
];
