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

// ==================== CONVERSATION QUERIES ====================

export const MY_CONVERSATIONS_QUERY = gql`
	query MyConversations($includeArchived: Boolean, $limit: Int, $offset: Int) {
		myConversations(includeArchived: $includeArchived, limit: $limit, offset: $offset) {
			id
			subject
			conversationType
			entityType
			entityId
			lastMessageAt
			isArchived
			createdAt
			updatedAt
			unreadCount
			participants {
				id
				participantType
				participantId
				isArchived
				isMuted
				participantProfile {
					__typename
					... on TeamProfileType {
						id
						fullName
						email
						role
					}
					... on CustomerProfileType {
						id
						fullName
						email
					}
				}
			}
			createdByProfile {
				__typename
				... on TeamProfileType {
					id
					fullName
				}
				... on CustomerProfileType {
					id
					fullName
				}
			}
		}
	}
`;

export const CONVERSATION_QUERY = gql`
	query Conversation($id: UUID!) {
		conversation(id: $id) {
			id
			subject
			conversationType
			entityType
			entityId
			lastMessageAt
			isArchived
			createdAt
			updatedAt
			unreadCount
			participants {
				id
				participantType
				participantId
				isArchived
				isMuted
				lastReadAt
				unreadCount
				participantProfile {
					__typename
					... on TeamProfileType {
						id
						fullName
						email
						role
					}
					... on CustomerProfileType {
						id
						fullName
						email
					}
				}
			}
			messages(limit: 100) {
				id
				content
				isDeleted
				isSystemMessage
				replyToId
				attachments
				createdAt
				authorType
				authorId
				authorProfile {
					__typename
					... on TeamProfileType {
						id
						fullName
					}
					... on CustomerProfileType {
						id
						fullName
					}
				}
			}
		}
	}
`;

export const CONVERSATIONS_BY_ENTITY_QUERY = gql`
	query ConversationsByEntity($entityType: String!, $entityId: UUID!) {
		conversationsByEntity(entityType: $entityType, entityId: $entityId) {
			id
			subject
			conversationType
			lastMessageAt
			isArchived
			createdAt
			unreadCount
			participants {
				id
				participantProfile {
					__typename
					... on TeamProfileType {
						id
						fullName
					}
					... on CustomerProfileType {
						id
						fullName
					}
				}
			}
		}
	}
`;

export const UNREAD_MESSAGE_COUNT_QUERY = gql`
	query UnreadMessageCount {
		unreadMessageCount
	}
`;

// ==================== MESSAGE QUERIES ====================

export const MESSAGES_QUERY = gql`
	query Messages(
		$conversationId: UUID!
		$limit: Int
		$offset: Int
		$includeDeleted: Boolean
		$includeSystem: Boolean
	) {
		messages(
			conversationId: $conversationId
			limit: $limit
			offset: $offset
			includeDeleted: $includeDeleted
			includeSystem: $includeSystem
		) {
			id
			conversationId
			content
			isDeleted
			isSystemMessage
			replyToId
			attachments
			createdAt
			authorType
			authorId
			authorProfile {
				__typename
				... on TeamProfileType {
					id
					fullName
				}
				... on CustomerProfileType {
					id
					fullName
				}
			}
			replyTo {
				id
				content
				authorProfile {
					__typename
					... on TeamProfileType {
						id
						fullName
					}
					... on CustomerProfileType {
						id
						fullName
					}
				}
			}
		}
	}
`;

export const MESSAGE_QUERY = gql`
	query Message($id: UUID!) {
		message(id: $id) {
			id
			conversationId
			content
			isDeleted
			isSystemMessage
			replyToId
			attachments
			createdAt
			authorType
			authorId
			authorProfile {
				__typename
				... on TeamProfileType {
					id
					fullName
				}
				... on CustomerProfileType {
					id
					fullName
				}
			}
		}
	}
`;

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

export type ConversationType = 'DIRECT' | 'GROUP' | 'SUPPORT';

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

export interface ConversationParticipant {
	id: string;
	participantType: string;
	participantId: string;
	isArchived: boolean;
	isMuted: boolean;
	lastReadAt?: string;
	unreadCount?: number;
	participantProfile: ProfileRef | null;
}

export interface Message {
	id: string;
	conversationId: string;
	content: string;
	isDeleted: boolean;
	isSystemMessage: boolean;
	replyToId: string | null;
	attachments: unknown | null;
	createdAt: string;
	authorType: string;
	authorId: string;
	authorProfile: ProfileRef | null;
	replyTo?: Message | null;
}

export interface Conversation {
	id: string;
	subject: string | null;
	conversationType: ConversationType;
	entityType: string | null;
	entityId: string | null;
	lastMessageAt: string | null;
	isArchived: boolean;
	createdAt: string;
	updatedAt: string;
	unreadCount: number;
	participants: ConversationParticipant[];
	createdByProfile: ProfileRef | null;
	messages?: Message[];
}

export interface MyConversationsQueryResult {
	myConversations: Conversation[];
}

export interface ConversationQueryResult {
	conversation: Conversation | null;
}

export interface ConversationsByEntityQueryResult {
	conversationsByEntity: Conversation[];
}

export interface UnreadMessageCountQueryResult {
	unreadMessageCount: number;
}

export interface MessagesQueryResult {
	messages: Message[];
}

export interface MessageQueryResult {
	message: Message | null;
}
