import { h } from 'vue';
import router from '../router';
import { getDocResource } from '../utils/resource';
import { unreadNotificationsCount } from '../data/notifications';
import { Tooltip, jingrowRequest } from 'jingrow-ui';
import { icon } from '../utils/components';
import { getTeam } from '../data/team';
import { toast } from 'vue-sonner';
import { t } from '../utils/i18n';

const getNotification = (name) => {
	return getDocResource({
		pagetype: 'Jcloud Notification',
		name: name,
		whitelistedMethods: {
			markNotificationAsRead: 'mark_as_read',
		},
	});
};

export default {
	pagetype: 'Jcloud Notification',
	whitelistedMethods: {},
	list: {
		resource() {
			let $team = getTeam();
			return {
				type: 'list',
				pagetype: 'Jcloud Notification',
				url: 'jcloud.api.notifications.get_notifications',
				auto: true,
				filters: {
					read: 'Unread',
				},
				cache: ['Notifications'],
			};
		},
		route: '/notifications',
		title: t('Notifications'),
		orderBy: 'creation desc',
		filterControls() {
			return [
				{
					type: 'tab',
					label: t('Read'),
					fieldname: 'read',
					options: [t('All'), t('Unread')],
					default: t('Unread'),
				},
			];
		},
		onRowClick(row) {
			const notification = getNotification(row.name);

			notification.markNotificationAsRead.submit().then(() => {
				unreadNotificationsCount.setData((data) => data - 1);
				if (row.route) router.push(row.route);
			});
		},
		actions({ listResource: notifications }) {
			return [
				{
					label: t('Mark All as Read'),
					slots: {
						prefix: icon('check-circle'),
					},
					async onClick() {
						toast.promise(
							jingrowRequest({
								url: '/api/action/jcloud.api.notifications.mark_all_notifications_as_read',
							}),
							{
								success: () => {
									notifications.reload();
									return t('All notifications marked as read');
								},
								loading: t('Marking all notifications as read...'),
								error: (error) =>
									error.messages?.length
										? error.messages.join('\n')
										: error.message,
							},
						);
					},
				},
			];
		},
		columns: [
			{
				label: t('Title'),
				fieldname: 'title',
				width: '20rem',
				format(value, row) {
					return value || row.type;
				},
				suffix(row) {
					if (row.is_actionable && !row.is_addressed) {
						let AlertIcon = icon('alert-circle');
						return h(
							Tooltip,
							{
								text: t('This notification requires your attention'),
							},
							{
								default: () =>
									h(
										'div',
										{
											class: 'ml-2  text-red-500',
										},
										h(AlertIcon),
									),
							},
						);
					}
				},
			},
			{
				label: t('Message'),
				fieldname: 'message',
				type: 'Component',
				width: '40rem',
				component({ row }) {
					return h('div', {
						class: 'truncate text-base text-gray-600',
						// replace all html tags except <b>
						innerHTML: row.message
							.replace(/<(?!\/?b\b)[^>]*>/g, '')
							.split('\n')[0],
					});
				},
			},
			{
				label: '',
				fieldname: 'creation',
				type: 'Timestamp',
				align: 'right',
			},
		],
	},
	routes: [],
};