import { gql } from '@apollo/client/core';

// ==================== USER NOTIFICATION QUERIES ====================

export const MY_NOTIFICATIONS_QUERY = gql`
	query MyNotifications($unreadOnly: Boolean, $limit: Int, $offset: Int) {
		myNotifications(unreadOnly: $unreadOnly, limit: $limit, offset: $offset) {
			id
			recipientType
			recipientId
			ruleId
			eventId
			status
			subject
			body
			actionUrl
			readAt
			isRead
			createdAt
			updatedAt
			event {
				id
				eventType
				entityType
				entityId
				timestamp
			}
		}
	}
`;

export const MY_UNREAD_NOTIFICATION_COUNT_QUERY = gql`
	query MyUnreadNotificationCount {
		myUnreadNotificationCount
	}
`;

export const NOTIFICATION_QUERY = gql`
	query Notification($id: UUID!) {
		notification(id: $id) {
			id
			recipientType
			recipientId
			ruleId
			eventId
			status
			subject
			body
			actionUrl
			readAt
			isRead
			metadata
			createdAt
			updatedAt
			recipientProfile {
				__typename
				... on TeamProfileType {
					id
					fullName
					email
				}
				... on CustomerProfileType {
					id
					fullName
					email
				}
			}
			event {
				id
				eventType
				entityType
				entityId
				actorType
				actorId
				timestamp
				metadata
				actorProfile {
					__typename
					... on TeamProfileType {
						id
						fullName
					}
					... on CustomerProfileType {
						id
						fullName
					}
				}
			}
			rule {
				id
				name
			}
			deliveries {
				id
				channel
				status
				attempts
				sentAt
				deliveredAt
				errorMessage
				isSuccessful
				isFailed
			}
		}
	}
`;

// ==================== ADMIN NOTIFICATION RULE QUERIES ====================

export const NOTIFICATION_RULES_QUERY = gql`
	query NotificationRules($isActive: Boolean) {
		notificationRules(isActive: $isActive) {
			id
			name
			description
			isActive
			eventTypes
			channels
			targetRoles
			conditions
			subjectTemplate
			bodyTemplate
			createdAt
			updatedAt
		}
	}
`;

export const NOTIFICATION_RULE_QUERY = gql`
	query NotificationRule($id: UUID!) {
		notificationRule(id: $id) {
			id
			name
			description
			isActive
			eventTypes
			channels
			targetRoles
			conditions
			subjectTemplate
			bodyTemplate
			createdAt
			updatedAt
			targetTeamProfiles {
				id
				fullName
				email
				role
			}
			targetCustomerProfiles {
				id
				fullName
				email
			}
		}
	}
`;

export const NOTIFICATION_DELIVERIES_QUERY = gql`
	query NotificationDeliveries($notificationId: UUID!) {
		notificationDeliveries(notificationId: $notificationId) {
			id
			notificationId
			channel
			status
			attempts
			lastAttemptAt
			sentAt
			deliveredAt
			errorMessage
			externalId
			isSuccessful
			isFailed
			createdAt
			updatedAt
		}
	}
`;

// ==================== TYPES ====================

export type NotificationStatus = 'PENDING' | 'SENT' | 'READ' | 'FAILED';
export type NotificationChannel = 'IN_APP' | 'EMAIL' | 'SMS';
export type DeliveryStatus =
	| 'PENDING'
	| 'QUEUED'
	| 'SENDING'
	| 'SENT'
	| 'DELIVERED'
	| 'FAILED'
	| 'BOUNCED';

export interface ProfileRef {
	__typename: 'TeamProfileType' | 'CustomerProfileType';
	id: string;
	fullName: string;
	email?: string;
	role?: string;
}

export interface NotificationEvent {
	id: string;
	eventType: string;
	entityType: string;
	entityId: string;
	actorType?: string;
	actorId?: string;
	timestamp: string;
	metadata?: unknown;
	actorProfile?: ProfileRef | null;
}

export interface NotificationDelivery {
	id: string;
	notificationId: string;
	channel: NotificationChannel;
	status: DeliveryStatus;
	attempts: number;
	lastAttemptAt: string | null;
	sentAt: string | null;
	deliveredAt: string | null;
	errorMessage: string | null;
	externalId: string | null;
	isSuccessful: boolean;
	isFailed: boolean;
	createdAt: string;
	updatedAt: string;
}

export interface NotificationRule {
	id: string;
	name: string;
	description: string | null;
	isActive: boolean;
	eventTypes: string[];
	channels: string[];
	targetRoles: string[] | null;
	conditions: unknown | null;
	subjectTemplate: string | null;
	bodyTemplate: string | null;
	createdAt: string;
	updatedAt: string;
	targetTeamProfiles?: Array<{
		id: string;
		fullName: string;
		email: string | null;
		role: string;
	}>;
	targetCustomerProfiles?: Array<{
		id: string;
		fullName: string;
		email: string | null;
	}>;
}

export interface Notification {
	id: string;
	recipientType: string;
	recipientId: string;
	ruleId: string | null;
	eventId: string | null;
	status: NotificationStatus;
	subject: string;
	body: string;
	actionUrl: string | null;
	readAt: string | null;
	isRead: boolean;
	metadata?: unknown;
	createdAt: string;
	updatedAt: string;
	recipientProfile?: ProfileRef | null;
	event?: NotificationEvent | null;
	rule?: { id: string; name: string } | null;
	deliveries?: NotificationDelivery[];
}

export interface MyNotificationsQueryResult {
	myNotifications: Notification[];
}

export interface MyUnreadNotificationCountQueryResult {
	myUnreadNotificationCount: number;
}

export interface NotificationQueryResult {
	notification: Notification | null;
}

export interface NotificationRulesQueryResult {
	notificationRules: NotificationRule[];
}

export interface NotificationRuleQueryResult {
	notificationRule: NotificationRule | null;
}

export interface NotificationDeliveriesQueryResult {
	notificationDeliveries: NotificationDelivery[];
}
