import { gql } from '@apollo/client/core';
import type { Notification, NotificationRule } from '../queries/notifications';

// ==================== USER NOTIFICATION MUTATIONS ====================

export const MARK_NOTIFICATION_AS_READ = gql`
	mutation MarkNotificationAsRead($id: UUID!) {
		markNotificationAsRead(id: $id) {
			id
			status
			readAt
			isRead
			updatedAt
		}
	}
`;

export const MARK_ALL_NOTIFICATIONS_AS_READ = gql`
	mutation MarkAllNotificationsAsRead {
		markAllNotificationsAsRead
	}
`;

export const DELETE_NOTIFICATION = gql`
	mutation DeleteNotification($id: UUID!) {
		deleteNotification(id: $id)
	}
`;

// ==================== ADMIN NOTIFICATION RULE MUTATIONS ====================

export const CREATE_NOTIFICATION_RULE = gql`
	mutation CreateNotificationRule($input: CreateNotificationRuleInput!) {
		createNotificationRule(input: $input) {
			id
			name
			description
			isActive
			eventTypes
			channels
			targetRoles
			conditions
			subjectTemplate
			bodyTemplate
			createdAt
			updatedAt
		}
	}
`;

export const UPDATE_NOTIFICATION_RULE = gql`
	mutation UpdateNotificationRule($id: UUID!, $input: UpdateNotificationRuleInput!) {
		updateNotificationRule(id: $id, input: $input) {
			id
			name
			description
			isActive
			eventTypes
			channels
			targetRoles
			conditions
			subjectTemplate
			bodyTemplate
			updatedAt
		}
	}
`;

export const DELETE_NOTIFICATION_RULE = gql`
	mutation DeleteNotificationRule($id: UUID!) {
		deleteNotificationRule(id: $id)
	}
`;

export const TOGGLE_NOTIFICATION_RULE = gql`
	mutation ToggleNotificationRule($id: UUID!, $isActive: Boolean!) {
		toggleNotificationRule(id: $id, isActive: $isActive) {
			id
			isActive
			updatedAt
		}
	}
`;

// ==================== INPUT TYPES ====================

export interface CreateNotificationRuleInput {
	name: string;
	description?: string;
	eventTypes: string[];
	channels: string[];
	targetRoles?: string[];
	conditions?: unknown;
	subjectTemplate?: string;
	bodyTemplate?: string;
	targetTeamProfileIds?: string[];
	targetCustomerProfileIds?: string[];
}

export interface UpdateNotificationRuleInput {
	name?: string;
	description?: string;
	eventTypes?: string[];
	channels?: string[];
	targetRoles?: string[];
	conditions?: unknown;
	subjectTemplate?: string;
	bodyTemplate?: string;
	targetTeamProfileIds?: string[];
	targetCustomerProfileIds?: string[];
}

// ==================== RESULT TYPES ====================

export interface MarkNotificationAsReadResult {
	markNotificationAsRead: Pick<Notification, 'id' | 'status' | 'readAt' | 'isRead' | 'updatedAt'>;
}

export interface MarkAllNotificationsAsReadResult {
	markAllNotificationsAsRead: number;
}

export interface DeleteNotificationResult {
	deleteNotification: boolean;
}

export interface CreateNotificationRuleResult {
	createNotificationRule: NotificationRule;
}

export interface UpdateNotificationRuleResult {
	updateNotificationRule: NotificationRule;
}

export interface DeleteNotificationRuleResult {
	deleteNotificationRule: boolean;
}

export interface ToggleNotificationRuleResult {
	toggleNotificationRule: Pick<NotificationRule, 'id' | 'isActive' | 'updatedAt'>;
}
