// Notification template metadata field definitions
// Based on backend metadata enrichment per event domain

export interface MetadataField {
	name: string;
	description: string;
}

export interface MetadataDomain {
	name: string;
	label: string;
	fields: MetadataField[];
	eventPrefixes: string[];
}

export const METADATA_DOMAINS: MetadataDomain[] = [
	{
		name: 'service',
		label: 'Service',
		eventPrefixes: ['SERVICE_'],
		fields: [
			{ name: 'date', description: 'Service date (e.g., 2025-01-15)' },
			{ name: 'account_name', description: 'Account name' },
			{ name: 'customer_name', description: 'Parent customer name' },
			{ name: 'address', description: 'Street and city' },
			{ name: 'service_id', description: 'Service UUID (session events only)' }
		]
	},
	{
		name: 'project',
		label: 'Project',
		eventPrefixes: ['PROJECT_'],
		fields: [
			{ name: 'project_name', description: 'Project name' },
			{ name: 'date', description: 'Project date' },
			{ name: 'account_name', description: 'Account name (or customer name if direct)' },
			{ name: 'customer_name', description: 'Parent customer name' },
			{ name: 'address', description: 'Project location' },
			{ name: 'project_id', description: 'Project UUID (session/punchlist events)' }
		]
	},
	{
		name: 'account',
		label: 'Account',
		eventPrefixes: ['ACCOUNT_'],
		fields: [
			{ name: 'account_name', description: 'Account name' },
			{ name: 'customer_name', description: 'Parent customer name' },
			{ name: 'account_id', description: 'Account UUID (address/contact/punchlist events)' },
			{ name: 'address', description: 'Address street and city (address events only)' }
		]
	},
	{
		name: 'customer',
		label: 'Customer',
		eventPrefixes: ['CUSTOMER_'],
		fields: [
			{ name: 'customer_name', description: 'Customer name' },
			{ name: 'customer_id', description: 'Customer UUID (address/contact events)' },
			{ name: 'address', description: 'Address street and city (address events only)' }
		]
	},
	{
		name: 'schedule',
		label: 'Schedule',
		eventPrefixes: ['SCHEDULE_'],
		fields: [
			{ name: 'account_name', description: 'Account name' },
			{ name: 'customer_name', description: 'Parent customer name' }
		]
	},
	{
		name: 'scope',
		label: 'Scope',
		eventPrefixes: ['SCOPE_', 'AREA_', 'TASK_'],
		fields: [
			{ name: 'account_name', description: 'Account name' },
			{ name: 'customer_name', description: 'Parent customer name' }
		]
	},
	{
		name: 'punchlist',
		label: 'Punchlist',
		eventPrefixes: ['PUNCHLIST_'],
		fields: [
			{ name: 'account_name', description: 'Account name' },
			{ name: 'customer_name', description: 'Parent customer name' },
			{ name: 'project_name', description: 'Project name (project punchlists only)' }
		]
	}
];

// Universal fields available for all events
export const UNIVERSAL_FIELDS: MetadataField[] = [
	{ name: 'event_type', description: 'The type of event (e.g., SERVICE_COMPLETED)' },
	{ name: 'entity_type', description: 'The type of entity (e.g., Service, Project)' },
	{ name: 'entity_id', description: 'The ID of the related entity' },
	{ name: 'timestamp', description: 'When the event occurred' }
];

/**
 * Get the relevant metadata domains based on selected event types
 */
export function getActiveDomainsForEvents(eventTypes: string[]): MetadataDomain[] {
	if (!eventTypes || eventTypes.length === 0) {
		return [];
	}

	const activeDomains = new Set<MetadataDomain>();

	for (const eventType of eventTypes) {
		for (const domain of METADATA_DOMAINS) {
			if (domain.eventPrefixes.some((prefix) => eventType.startsWith(prefix))) {
				activeDomains.add(domain);
				break;
			}
		}
	}

	return Array.from(activeDomains);
}
