import { defineAsyncComponent, h } from 'vue';
import LucideServer from '~icons/lucide/server';
import { getTeam } from '../data/team';
import router from '../router';
import { icon } from '../utils/components';
import { duration, planTitle, userCurrency } from '../utils/format';
import { trialDays } from '../utils/site';
import { getJobsTab } from './common/jobs';
import { tagTab } from './common/tags';
import { t } from '../utils/i18n';

export default {
	pagetype: 'Jsite Server',
	whitelistedMethods: {
		reboot: 'reboot',
		rename: 'rename',
		dropServer: 'drop_server',
		addTag: 'add_resource_tag',
		removeTag: 'remove_resource_tag'
	},
	list: {
		route: '/jsite-servers',
		title: t('Servers'),
		fields: [
			'name',
			'title',
			'status',
			'region',
			'cpu',
			'memory',
			'disk_size',
			'public_ip',
			'private_ip',
			'end_date',
			'bandwidth',
			'team',
			'instance_id',
			'order_id',
			'planid',
			'image_id',
			'system'
		],
		filterControls() {
			return [
				{
					type: 'select',
					label: t('Status'),
					fieldname: 'status',
					options: [
						{ label: '', value: '' },
						{ label: t('Pending'), value: 'Pending' },
						{ label: t('Starting'), value: 'Starting' },
						{ label: t('Running'), value: 'Running' },
						{ label: t('Stopping'), value: 'Stopping' },
						{ label: t('Stopped'), value: 'Stopped' },
						{ label: t('Resetting'), value: 'Resetting' },
						{ label: t('Upgrading'), value: 'Upgrading' },
						{ label: t('Disabled'), value: 'Disabled' }
					]
				},
				{
					type: 'select',
					label: t('Region'),
					fieldname: 'region',
					options: [
						{ label: '', value: '' },
						{ label: t('North China 1 (Qingdao)'), value: 'cn-qingdao' },
						{ label: t('North China 2 (Beijing)'), value: 'cn-beijing' },
						{ label: t('North China 3 (Zhangjiakou)'), value: 'cn-zhangjiakou' },
						{ label: t('North China 5 (Hohhot)'), value: 'cn-huhehaote' },
						{ label: t('East China 1 (Hangzhou)'), value: 'cn-hangzhou' },
						{ label: t('East China 2 (Shanghai)'), value: 'cn-shanghai' },
						{ label: t('South China 1 (Shenzhen)'), value: 'cn-shenzhen' },
						{ label: t('South China 2 (Heyuan)'), value: 'cn-heyuan' },
						{ label: t('Southwest China 1 (Chengdu)'), value: 'cn-chengdu' },
						{ label: t('South China 3 (Guangzhou)'), value: 'cn-guangzhou' },
						{ label: t('North China 6 (Ulanqab)'), value: 'cn-wulanchabu' },
						{ label: t('East China 5 (Nanjing)'), value: 'cn-nanjing' },
						{ label: t('East China 6 (Fuzhou)'), value: 'cn-fuzhou' },
						{ label: t('Central China 1 (Wuhan)'), value: 'cn-wuhan-lr' },
						{ label: t('Hong Kong'), value: 'cn-hongkong' },
						{ label: t('Singapore'), value: 'ap-southeast-1' },
						{ label: t('Malaysia (Kuala Lumpur)'), value: 'ap-southeast-3' },
						{ label: t('Indonesia (Jakarta)'), value: 'ap-southeast-5' },
						{ label: t('Japan (Tokyo)'), value: 'ap-northeast-1' },
						{ label: t('US (Silicon Valley)'), value: 'us-west-1' },
						{ label: t('US (Virginia)'), value: 'us-east-1' },
						{ label: t('Germany (Frankfurt)'), value: 'eu-central-1' },
						{ label: t('UK (London)'), value: 'eu-west-1' },
						{ label: t('Philippines (Manila)'), value: 'ap-southeast-6' },
						{ label: t('Thailand (Bangkok)'), value: 'ap-southeast-7' },
						{ label: t('South Korea (Seoul)'), value: 'ap-northeast-2' }
					]
				}
			];
		},
		orderBy: 'creation desc',
		searchField: 'title',
		columns: [
			{
				label: t('Server'),
				fieldname: 'name',
				width: 1.5,
				class: 'font-medium',
				format(value, row) {
					return row.title || value;
				}
			},
			{ 
				label: t('Status'), 
				fieldname: 'status', 
				type: 'Badge', 
				width: 0.8,
				format(value) {
					const statusMap = {
						'Pending': t('Pending'),
						'Starting': t('Starting'),
						'Running': t('Running'),
						'Stopping': t('Stopping'),
						'Stopped': t('Stopped'),
						'Resetting': t('Resetting'),
						'Upgrading': t('Upgrading'),
						'Disabled': t('Disabled')
					};
					return statusMap[value] || value;
				}
			},
			{
				label: t('Configuration'),
				fieldname: 'cpu',
				format(value, row) {
					const cpu = row.cpu || t('Unknown');
					const memory = row.memory || t('Unknown');
					const disk = row.disk_size || t('Unknown');
					return `${cpu}${t('cores')}/${memory}GB/${disk}GB`;
				}
			},
			{
				label: t('Public IP'),
				fieldname: 'public_ip',
				format(value) {
					return value || '-';
				}
			},
			{
				label: t('Region'),
				fieldname: 'region',
				format(value) {
					if (!value) return '-';
					
					// Region ID to name mapping
					const regionMap = {
						'cn-qingdao': t('North China 1 (Qingdao)'),
						'cn-beijing': t('North China 2 (Beijing)'),
						'cn-zhangjiakou': t('North China 3 (Zhangjiakou)'),
						'cn-huhehaote': t('North China 5 (Hohhot)'),
						'cn-hangzhou': t('East China 1 (Hangzhou)'),
						'cn-shanghai': t('East China 2 (Shanghai)'),
						'cn-shenzhen': t('South China 1 (Shenzhen)'),
						'cn-heyuan': t('South China 2 (Heyuan)'),
						'cn-chengdu': t('Southwest China 1 (Chengdu)'),
						'cn-guangzhou': t('South China 3 (Guangzhou)'),
						'cn-wulanchabu': t('North China 6 (Ulanqab)'),
						'cn-nanjing': t('East China 5 (Nanjing)'),
						'cn-fuzhou': t('East China 6 (Fuzhou)'),
						'cn-wuhan-lr': t('Central China 1 (Wuhan)'),
						'cn-hongkong': t('Hong Kong'),
						'ap-southeast-1': t('Singapore'),
						'ap-southeast-3': t('Malaysia (Kuala Lumpur)'),
						'ap-southeast-5': t('Indonesia (Jakarta)'),
						'ap-northeast-1': t('Japan (Tokyo)'),
						'us-west-1': t('US (Silicon Valley)'),
						'us-east-1': t('US (Virginia)'),
						'eu-central-1': t('Germany (Frankfurt)'),
						'eu-west-1': t('UK (London)'),
						'ap-southeast-6': t('Philippines (Manila)'),
						'ap-southeast-7': t('Thailand (Bangkok)'),
						'ap-northeast-2': t('South Korea (Seoul)')
					};
					
					return regionMap[value] || value;
				}
			},
			{
				label: t('Expiry Date'),
				fieldname: 'end_date',
				format(value) {
					if (!value) return '-';
					return value;
				}
			}
		],
		primaryAction({ listResource: jsiteServers }) {
			return {
				label: t('New Server'),
				variant: 'outline',
				slots: {
					prefix: icon('plus')
				},
				onClick() {
					router.push('/jsite-servers/new');
				}
			};
		},
		statusBadge({ documentResource: jsiteServer }) {
			const status = jsiteServer.pg?.status;
			const statusMap = {
				'Pending': t('Pending'),
				'Starting': t('Starting'),
				'Running': t('Running'),
				'Stopping': t('Stopping'),
				'Stopped': t('Stopped'),
				'Resetting': t('Resetting'),
				'Upgrading': t('Upgrading'),
				'Disabled': t('Disabled')
			};
			return {
				label: statusMap[status] || status
			};
		},
		breadcrumbs({ documentResource: jsiteServer }) {
			return [
				{
					label: t('Servers'),
					route: '/jsite-servers'
				},
				{
					label: jsiteServer.pg?.title || jsiteServer.pg?.name,
					route: `/jsite-servers/${jsiteServer.pg?.name}`
				}
			];
		},
		actions({ documentResource: jsiteServer }) {
			if (!jsiteServer) return [];
			
			const actions = [
				{
					label: t('Restart'),
					icon: 'refresh-cw',
					onClick() {
						jsiteServer.reboot.submit();
					},
					condition: () => jsiteServer.pg?.status === 'Running'
				},
				{
					label: t('Rename'),
					icon: 'edit-3',
					onClick() {
						jsiteServer.rename.submit();
					}
				},
				{
					label: t('Delete'),
					icon: 'trash-2',
					onClick() {
						jsiteServer.dropServer.submit();
					},
					condition: () => jsiteServer.pg?.status !== 'Running'
				}
			];
			return actions.filter(action => !action.condition || action.condition());
		}
	},
	detail: {
		route: '/jsite-servers/:name',
		title: t('Server Details'),
		tabs: [
			{
				label: t('Overview'),
				route: '',
				type: 'Component',
				component: defineAsyncComponent(() => import('../components/JsiteServerOverview.vue')),
				props: jsiteServer => {
					return { server: jsiteServer.pg?.name };
				}
			},
			{
				label: t('Firewall'),
				route: 'firewall',
				type: 'Component',
				component: defineAsyncComponent(() => import('../components/JsiteServerFirewallRules.vue')),
				props: jsiteServer => {
					return { server: jsiteServer.pg?.name };
				}
			}
		],
		fields: [
			{
				label: t('Basic Information'),
				fields: [
					'title',
					'status',
					'region',
					'instance_id',
					'order_id'
				]
			},
			{
				label: t('Server Configuration'),
				fields: [
					'cpu',
					'memory',
					'disk_size',
					'bandwidth',
					'public_ip',
					'image_id',
					'planid',
					'system'
				]
			},
			{
				label: t('SSH Connection'),
				fields: [
					'ssh_user',
					'ssh_port',
					'password',
					'key_pair_name',
					'private_key'
				]
			},
			{
				label: t('Other Information'),
				fields: [
					'end_date',
					'period'
				]
			}
		],
		actions({ documentResource: jsiteServer }) {
			return [];
		}
	}
}; 