import { INodeProperties } from 'n8n-workflow';

export const billsOperations: INodeProperties[] = [
	{
		displayName: 'Operation',
		name: 'operation',
		type: 'options',
		noDataExpression: true,
		displayOptions: {
			show: {
				resource: ['bills'],
			},
		},
		options: [
			{
				name: 'Get All',
				value: 'getAll',
				description: 'Get all bills',
				action: 'Get all bills',
				routing: {
					request: {
						method: 'GET',
						url: '/bills',
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'bills',
								},
							},
						],
					},
				},
			},
			{
				name: 'Get',
				value: 'get',
				description: 'Get a specific bill',
				action: 'Get a bill',
				routing: {
					request: {
						method: 'GET',
						url: '=/bills/{{$parameter.billId}}',
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'bills',
								},
							},
						],
					},
				},
			},
			{
				name: 'Get by Reference',
				value: 'getByRef',
				description: 'Get bill by reference',
				action: 'Get bill by reference',
				routing: {
					request: {
						method: 'GET',
						url: '/bills',
						qs: {
							ref: '={{$parameter.billRef}}',
						},
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'bills',
								},
							},
						],
					},
				},
			},
			{
				name: 'Get by Customer ID',
				value: 'getByCustId',
				description: 'Get bill by customer ID',
				action: 'Get bill by customer ID',
				routing: {
					request: {
						method: 'GET',
						url: '/bills',
						qs: {
							custid: '={{$parameter.custId}}',
						},
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'bills',
								},
							},
						],
					},
				},
			},
			{
				name: 'Create or Update',
				value: 'createOrUpdate',
				description: 'Create a new bill or update existing',
				action: 'Create or update bill',
				routing: {
					request: {
						method: 'POST',
						url: '/bills',
					},
					send: {
						type: 'body',
					},
				},
			},
			{
				name: 'Delete',
				value: 'delete',
				description: 'Delete a bill',
				action: 'Delete a bill',
				routing: {
					request: {
						method: 'DELETE',
						url: '=/bills/{{$parameter.billId}}',
					},
				},
			},
			{
				name: 'Get History',
				value: 'getHistory',
				description: 'Get bill history',
				action: 'Get bill history',
				routing: {
					request: {
						method: 'GET',
						url: '=/bills/{{$parameter.billId}}/history',
					},
					output: {
						postReceive: [
							{
								type: 'rootProperty',
								properties: {
									property: 'bill_history',
								},
							},
						],
					},
				},
			},
		],
		default: 'getAll',
	},
];

export const billsFields: INodeProperties[] = [
	// Bill ID for specific operations
	{
		displayName: 'Bill ID',
		name: 'billId',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['get', 'delete', 'getHistory'],
			},
		},
		default: '',
		description: 'The bill ID',
	},

	// Bill reference
	{
		displayName: 'Bill Reference',
		name: 'billRef',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['getByRef'],
			},
		},
		default: '',
		description: 'Bill reference identifier',
	},

	// Customer ID
	{
		displayName: 'Customer ID',
		name: 'custId',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['getByCustId'],
			},
		},
		default: '',
		description: 'Customer ID',
	},

	// Fields for creating/updating bills
	{
		displayName: 'Bill Name',
		name: 'bill_name',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['createOrUpdate'],
			},
		},
		default: '',
		description: 'Name of the bill',
	},
	{
		displayName: 'Bill Day',
		name: 'bill_day',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['createOrUpdate'],
			},
		},
		default: '1',
		description: 'Day of month for billing cycle',
	},
	{
		displayName: 'Bill Type',
		name: 'bill_type',
		type: 'options',
		required: true,
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['createOrUpdate'],
			},
		},
		options: [
			{ name: 'CDR', value: 'cdr' },
			{ name: 'Quota', value: 'quota' },
		],
		default: 'cdr',
		description: 'Type of billing',
	},
	{
		displayName: 'Ports',
		name: 'ports',
		type: 'string',
		required: true,
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['createOrUpdate'],
			},
		},
		default: '',
		placeholder: '1021,1022,1023',
		description: 'Comma-separated list of port IDs',
	},

	// Additional bill options
	{
		displayName: 'Additional Options',
		name: 'additionalOptions',
		type: 'collection',
		placeholder: 'Add Option',
		default: {},
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['createOrUpdate'],
			},
		},
		options: [
			{
				displayName: 'Bill ID (for update)',
				name: 'bill_id',
				type: 'string',
				default: '',
				description: 'Bill ID for updating existing bill',
			},
			{
				displayName: 'CDR Value',
				name: 'bill_cdr',
				type: 'string',
				default: '',
				description: 'CDR value for CDR type bills',
			},
			{
				displayName: 'Quota',
				name: 'bill_quota',
				type: 'string',
				default: '',
				description: 'Quota value for quota type bills',
			},
			{
				displayName: 'Customer ID',
				name: 'bill_custid',
				type: 'string',
				default: '',
				description: 'Customer identifier',
			},
			{
				displayName: 'Bill Reference',
				name: 'bill_ref',
				type: 'string',
				default: '',
				description: 'Bill reference',
			},
			{
				displayName: 'Notes',
				name: 'bill_notes',
				type: 'string',
				default: '',
				description: 'Notes for the bill',
			},
		],
	},

	// Period option for getAll and specific gets
	{
		displayName: 'Options',
		name: 'options',
		type: 'collection',
		placeholder: 'Add Option',
		default: {},
		displayOptions: {
			show: {
				resource: ['bills'],
				operation: ['getAll', 'get', 'getByRef', 'getByCustId'],
			},
		},
		options: [
			{
				displayName: 'Period',
				name: 'period',
				type: 'options',
				options: [
					{ name: 'Current', value: '' },
					{ name: 'Previous', value: 'previous' },
				],
				default: '',
				description: 'Billing period to retrieve',
				routing: {
					send: {
						type: 'query',
						property: 'period',
					},
				},
			},
		],
	},
];
