import { INodeProperties } from 'n8n-workflow';
import {
	nodeNameOption,
	vmIdOption,
	waitForTaskOption,
	taskTimeoutOption,
	returnAllOption,
	limitOption,
} from './baseProperties';

export const containerOperations: INodeProperties = {
	displayName: 'Operation',
	name: 'operation',
	type: 'options',
	noDataExpression: true,
	displayOptions: {
		show: {
			resource: ['container'],
		},
	},
	options: [
		{
			name: 'Create',
			value: 'create',
			description: 'Create a new LXC container',
			action: 'Create a container',
		},
		{
			name: 'Delete',
			value: 'delete',
			description: 'Delete an LXC container',
			action: 'Delete a container',
		},
		{
			name: 'Get',
			value: 'get',
			description: 'Get container details',
			action: 'Get a container',
		},
		{
			name: 'Get All',
			value: 'getAll',
			description: 'Get all containers',
			action: 'Get all containers',
		},
		{
			name: 'Start',
			value: 'start',
			description: 'Start a container',
			action: 'Start a container',
		},
		{
			name: 'Stop',
			value: 'stop',
			description: 'Stop a container',
			action: 'Stop a container',
		},
		{
			name: 'Shutdown',
			value: 'shutdown',
			description: 'Gracefully shutdown a container',
			action: 'Shutdown a container',
		},
		{
			name: 'Reboot',
			value: 'reboot',
			description: 'Reboot a container',
			action: 'Reboot a container',
		},
		{
			name: 'Suspend',
			value: 'suspend',
			description: 'Suspend a container',
			action: 'Suspend a container',
		},
		{
			name: 'Resume',
			value: 'resume',
			description: 'Resume a suspended container',
			action: 'Resume a container',
		},
		{
			name: 'Clone',
			value: 'clone',
			description: 'Clone a container',
			action: 'Clone a container',
		},
		{
			name: 'Migrate',
			value: 'migrate',
			description: 'Migrate a container to another node',
			action: 'Migrate a container',
		},
		{
			name: 'Create Snapshot',
			value: 'snapshot',
			description: 'Create a snapshot of the container',
			action: 'Create container snapshot',
		},
		{
			name: 'Get Status',
			value: 'getStatus',
			description: 'Get current status of a container',
			action: 'Get container status',
		},
	],
	default: 'getAll',
};

export const containerFields: INodeProperties[] = [
	// Common fields
	{
		displayName: nodeNameOption.displayName,
		name: nodeNameOption.name,
		type: nodeNameOption.type,
		required: nodeNameOption.required,
		default: nodeNameOption.default,
		placeholder: nodeNameOption.placeholder,
		description: nodeNameOption.description,
		displayOptions: {
			show: {
				resource: ['container'],
				operation: [
					'create',
					'delete',
					'get',
					'start',
					'stop',
					'shutdown',
					'reboot',
					'suspend',
					'resume',
					'clone',
					'migrate',
					'snapshot',
					'getStatus',
				],
			},
		},
	},
	{
		displayName: 'Container ID',
		name: vmIdOption.name,
		type: vmIdOption.type,
		required: vmIdOption.required,
		default: vmIdOption.default,
		typeOptions: vmIdOption.typeOptions,
		description: 'Unique ID of the container',
		displayOptions: {
			show: {
				resource: ['container'],
				operation: [
					'delete',
					'get',
					'start',
					'stop',
					'shutdown',
					'reboot',
					'suspend',
					'resume',
					'clone',
					'migrate',
					'snapshot',
					'getStatus',
				],
			},
		},
	},

	// Create container specific fields
	{
		displayName: 'Container ID',
		name: 'newVmId',
		type: 'number',
		required: true,
		default: 100,
		typeOptions: {
			minValue: 100,
			maxValue: 999999999,
		},
		description: 'Unique ID for the new container',
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['create'],
			},
		},
	},
	{
		displayName: 'Container Template',
		name: 'ostemplate',
		type: 'string',
		required: true,
		default: '',
		placeholder: 'ubuntu-22.04-standard_22.04-1_amd64.tar.zst',
		description: 'Container template to use',
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['create'],
			},
		},
	},

	// Clone specific fields
	{
		displayName: 'New Container ID',
		name: 'newVmId',
		type: 'number',
		required: true,
		default: 101,
		typeOptions: {
			minValue: 100,
			maxValue: 999999999,
		},
		description: 'ID for the cloned container',
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['clone'],
			},
		},
	},
	{
		displayName: 'Clone Name',
		name: 'cloneName',
		type: 'string',
		default: '',
		placeholder: 'cloned-container',
		description: 'Name for the cloned container',
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['clone'],
			},
		},
	},

	// Migration fields
	{
		displayName: 'Target Node',
		name: 'targetNode',
		type: 'string',
		required: true,
		default: '',
		placeholder: 'pve-node-02',
		description: 'Target node for migration',
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['migrate'],
			},
		},
	},

	// Snapshot fields
	{
		displayName: 'Snapshot Name',
		name: 'snapshotName',
		type: 'string',
		required: true,
		default: '',
		placeholder: 'snapshot-1',
		description: 'Name for the snapshot',
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['snapshot'],
			},
		},
	},

	// Wait for task options
	{
		displayName: waitForTaskOption.displayName,
		name: waitForTaskOption.name,
		type: waitForTaskOption.type,
		default: waitForTaskOption.default,
		description: waitForTaskOption.description,
		displayOptions: {
			show: {
				resource: ['container'],
				operation: [
					'create',
					'delete',
					'start',
					'stop',
					'shutdown',
					'reboot',
					'suspend',
					'resume',
					'clone',
					'migrate',
					'snapshot',
				],
			},
		},
	},
	{
		displayName: taskTimeoutOption.displayName,
		name: taskTimeoutOption.name,
		type: taskTimeoutOption.type,
		default: taskTimeoutOption.default,
		typeOptions: taskTimeoutOption.typeOptions,
		description: taskTimeoutOption.description,
		displayOptions: {
			show: {
				resource: ['container'],
				operation: [
					'create',
					'delete',
					'start',
					'stop',
					'shutdown',
					'reboot',
					'suspend',
					'resume',
					'clone',
					'migrate',
					'snapshot',
				],
				waitForTask: [true],
			},
		},
	},

	// Get All options
	{
		displayName: returnAllOption.displayName,
		name: returnAllOption.name,
		type: returnAllOption.type,
		default: returnAllOption.default,
		description: returnAllOption.description,
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['getAll'],
			},
		},
	},
	{
		displayName: limitOption.displayName,
		name: limitOption.name,
		type: limitOption.type,
		typeOptions: limitOption.typeOptions,
		default: limitOption.default,
		description: limitOption.description,
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['getAll'],
				returnAll: [false],
			},
		},
	},

	// Additional options for create and clone
	{
		displayName: 'Additional Options',
		name: 'additionalOptions',
		type: 'collection',
		placeholder: 'Add Option',
		default: {},
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['create', 'clone'],
			},
		},
		options: [
			{
				displayName: 'Container Name',
				name: 'hostname',
				type: 'string',
				default: '',
				placeholder: 'my-container',
				description: 'Hostname for the container',
			},
			{
				displayName: 'Memory (MB)',
				name: 'memory',
				type: 'number',
				default: 512,
				typeOptions: {
					minValue: 16,
				},
				description: 'Amount of RAM for the container in MB',
			},
			{
				displayName: 'Swap (MB)',
				name: 'swap',
				type: 'number',
				default: 512,
				typeOptions: {
					minValue: 0,
				},
				description: 'Amount of swap space in MB',
			},
			{
				displayName: 'CPU Cores',
				name: 'cores',
				type: 'number',
				default: 1,
				typeOptions: {
					minValue: 1,
					maxValue: 128,
				},
				description: 'Number of CPU cores',
			},
			{
				displayName: 'Disk Size (GB)',
				name: 'rootfs',
				type: 'string',
				default: '8',
				description: 'Size of the root filesystem in GB',
			},
			{
				displayName: 'Storage',
				name: 'storage',
				type: 'string',
				default: 'local-lvm',
				description: 'Storage location for the container',
			},
			{
				displayName: 'Network Configuration',
				name: 'net0',
				type: 'string',
				default: 'name=eth0,bridge=vmbr0,firewall=1,ip=dhcp',
				description: 'Network configuration for the container',
			},
			{
				displayName: 'Password',
				name: 'password',
				type: 'string',
				typeOptions: { password: true },
				default: '',
				description: 'Root password for the container',
			},
			{
				displayName: 'SSH Public Key',
				name: 'ssh_public_keys',
				type: 'string',
				default: '',
				description: 'SSH public key to add to the container',
			},
			{
				displayName: 'Unprivileged',
				name: 'unprivileged',
				type: 'boolean',
				default: true,
				description: 'Create an unprivileged container',
			},
			{
				displayName: 'Start After Create',
				name: 'startAfterCreate',
				type: 'boolean',
				default: false,
				description: 'Whether to start the container after creation',
			},
		],
	},

	// Additional options for various operations
	{
		displayName: 'Additional Options',
		name: 'additionalOptions',
		type: 'collection',
		placeholder: 'Add Option',
		default: {},
		displayOptions: {
			show: {
				resource: ['container'],
				operation: ['stop', 'shutdown', 'migrate', 'snapshot'],
			},
		},
		options: [
			{
				displayName: 'Force',
				name: 'force',
				type: 'boolean',
				default: false,
				description: 'Force the operation (use with caution)',
			},
			{
				displayName: 'Skip Lock',
				name: 'skipLock',
				type: 'boolean',
				default: false,
				description: 'Skip container lock check',
			},
			{
				displayName: 'Online Migration',
				name: 'online',
				type: 'boolean',
				default: true,
				description: 'Use online migration (only for migrate operation)',
			},
			{
				displayName: 'Restart Services',
				name: 'restart',
				type: 'boolean',
				default: false,
				description: 'Restart services after migration',
			},
		],
	},
];
