import { gql } from '@apollo/client/core';
import type {
	Conversation,
	ConversationParticipant,
	Message,
	ConversationType
} from '../queries/messaging';

// ==================== CONVERSATION MUTATIONS ====================

export const CREATE_CONVERSATION = gql`
	mutation CreateConversation($input: CreateConversationInput!) {
		createConversation(input: $input) {
			id
			subject
			conversationType
			entityType
			entityId
			lastMessageAt
			isArchived
			createdAt
			participants {
				id
				participantType
				participantId
				participantProfile {
					__typename
					... on TeamProfileType {
						id
						fullName
					}
					... on CustomerProfileType {
						id
						fullName
					}
				}
			}
		}
	}
`;

export const UPDATE_CONVERSATION = gql`
	mutation UpdateConversation($id: UUID!, $input: UpdateConversationInput!) {
		updateConversation(id: $id, input: $input) {
			id
			subject
			updatedAt
		}
	}
`;

export const ARCHIVE_CONVERSATION = gql`
	mutation ArchiveConversation($id: UUID!, $isArchived: Boolean!) {
		archiveConversation(id: $id, isArchived: $isArchived) {
			id
			isArchived
			updatedAt
		}
	}
`;

export const MUTE_CONVERSATION = gql`
	mutation MuteConversation($id: UUID!, $isMuted: Boolean!) {
		muteConversation(id: $id, isMuted: $isMuted)
	}
`;

export const LEAVE_CONVERSATION = gql`
	mutation LeaveConversation($conversationId: UUID!) {
		leaveConversation(conversationId: $conversationId)
	}
`;

// ==================== MESSAGE MUTATIONS ====================

export const SEND_MESSAGE = gql`
	mutation SendMessage($input: SendMessageInput!) {
		sendMessage(input: $input) {
			id
			conversationId
			content
			isDeleted
			isSystemMessage
			replyToId
			attachments
			createdAt
			authorType
			authorId
			authorProfile {
				__typename
				... on TeamProfileType {
					id
					fullName
				}
				... on CustomerProfileType {
					id
					fullName
				}
			}
		}
	}
`;

export const DELETE_MESSAGE = gql`
	mutation DeleteMessage($id: UUID!) {
		deleteMessage(id: $id)
	}
`;

// ==================== READ TRACKING MUTATIONS ====================

export const MARK_CONVERSATION_AS_READ = gql`
	mutation MarkConversationAsRead($id: UUID!) {
		markConversationAsRead(id: $id)
	}
`;

export const MARK_ALL_CONVERSATIONS_AS_READ = gql`
	mutation MarkAllConversationsAsRead {
		markAllConversationsAsRead
	}
`;

// ==================== PARTICIPANT MUTATIONS ====================

export const ADD_PARTICIPANT = gql`
	mutation AddParticipant($conversationId: UUID!, $participant: ParticipantInput!) {
		addParticipant(conversationId: $conversationId, participant: $participant) {
			id
			conversationId
			participantType
			participantId
			isArchived
			joinedAt
			participantProfile {
				__typename
				... on TeamProfileType {
					id
					fullName
				}
				... on CustomerProfileType {
					id
					fullName
				}
			}
		}
	}
`;

export const REMOVE_PARTICIPANT = gql`
	mutation RemoveParticipant($conversationId: UUID!, $participant: ParticipantInput!) {
		removeParticipant(conversationId: $conversationId, participant: $participant)
	}
`;

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

export interface ParticipantInput {
	participantType: string;
	participantId: string;
}

export interface CreateConversationInput {
	subject?: string;
	conversationType: ConversationType;
	entityType?: string;
	entityId?: string;
	participantIds: ParticipantInput[];
	initialMessage?: string;
}

export interface UpdateConversationInput {
	subject?: string;
}

export interface SendMessageInput {
	conversationId: string;
	content: string;
	replyToId?: string;
	attachments?: unknown;
}

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

export interface CreateConversationResult {
	createConversation: Conversation;
}

export interface UpdateConversationResult {
	updateConversation: Pick<Conversation, 'id' | 'subject' | 'updatedAt'>;
}

export interface ArchiveConversationResult {
	archiveConversation: Pick<Conversation, 'id' | 'isArchived' | 'updatedAt'>;
}

export interface MuteConversationResult {
	muteConversation: boolean;
}

export interface LeaveConversationResult {
	leaveConversation: boolean;
}

export interface SendMessageResult {
	sendMessage: Message;
}

export interface DeleteMessageResult {
	deleteMessage: boolean;
}

export interface MarkConversationAsReadResult {
	markConversationAsRead: boolean;
}

export interface MarkAllConversationsAsReadResult {
	markAllConversationsAsRead: number;
}

export interface AddParticipantResult {
	addParticipant: ConversationParticipant;
}

export interface RemoveParticipantResult {
	removeParticipant: boolean;
}
