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

// ==================== SESSION FRAGMENTS ====================

export const SESSION_NOTE_FRAGMENT = gql`
	fragment SessionNote on ServiceSessionNoteType {
		id
		sessionId
		content
		authorId
		internal
		createdAt
		updatedAt
		author {
			id
			fullName
		}
	}
`;

export const SESSION_IMAGE_FRAGMENT = gql`
	fragment SessionImage on ServiceSessionImageType {
		id
		sessionId
		title
		image
		thumbnail
		contentType
		width
		height
		uploadedById
		notes
		internal
		createdAt
		uploadedBy {
			id
			fullName
		}
	}
`;

export const SESSION_VIDEO_FRAGMENT = gql`
	fragment SessionVideo on ServiceSessionVideoType {
		id
		sessionId
		title
		video
		thumbnail
		contentType
		width
		height
		durationSeconds
		fileSizeBytes
		uploadedById
		notes
		internal
		createdAt
		uploadedBy {
			id
			fullName
		}
	}
`;

export const TASK_COMPLETION_FRAGMENT = gql`
	fragment TaskCompletion on ServiceTaskCompletionType {
		id
		sessionId
		taskId
		completedById
		completedAt
		notes
		task {
			id
			scopeDescription
			sessionDescription
			frequency
			order
		}
		completedBy {
			id
			fullName
		}
	}
`;

// ==================== SERVICE SESSION QUERIES ====================

export const SERVICE_SESSION_QUERY = gql`
	query ServiceSession($id: UUID!) {
		serviceSession(id: $id) {
			id
			serviceId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			end
			date
			createdById
			closedById
			createdAt
			updatedAt
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			closedBy {
				id
				fullName
			}
			notes {
				id
				sessionId
				content
				authorId
				internal
				createdAt
				updatedAt
				author {
					id
					fullName
				}
			}
			images {
				id
				sessionId
				title
				image
				thumbnail
				contentType
				width
				height
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			videos {
				id
				sessionId
				title
				video
				thumbnail
				contentType
				width
				height
				durationSeconds
				fileSizeBytes
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			completedTasks {
				id
				sessionId
				taskId
				completedById
				completedAt
				notes
				task {
					id
					scopeDescription
					sessionDescription
					frequency
					order
				}
				completedBy {
					id
					fullName
				}
			}
		}
	}
`;

export const SERVICE_SESSIONS_QUERY = gql`
	query ServiceSessions($serviceId: UUID!) {
		serviceSessions(serviceId: $serviceId) {
			id
			serviceId
			start
			end
			date
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			closedBy {
				id
				fullName
			}
		}
	}
`;

export const LATEST_SERVICE_SESSION_QUERY = gql`
	query LatestServiceSession($serviceId: UUID!) {
		latestServiceSession(serviceId: $serviceId) {
			id
			serviceId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			end
			date
			createdById
			closedById
			createdAt
			updatedAt
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			closedBy {
				id
				fullName
			}
			notes {
				id
				sessionId
				content
				authorId
				internal
				createdAt
				updatedAt
				author {
					id
					fullName
				}
			}
			images {
				id
				sessionId
				title
				image
				thumbnail
				contentType
				width
				height
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			videos {
				id
				sessionId
				title
				video
				thumbnail
				contentType
				width
				height
				durationSeconds
				fileSizeBytes
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			completedTasks {
				id
				sessionId
				taskId
				completedById
				completedAt
				notes
				task {
					id
					scopeDescription
					sessionDescription
					frequency
					order
				}
				completedBy {
					id
					fullName
				}
			}
		}
	}
`;

export const ACTIVE_SERVICE_SESSION_QUERY = gql`
	query ActiveServiceSession($serviceId: UUID!) {
		activeServiceSession(serviceId: $serviceId) {
			id
			serviceId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			end
			date
			createdById
			closedById
			createdAt
			updatedAt
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			notes {
				id
				sessionId
				content
				authorId
				internal
				createdAt
				updatedAt
				author {
					id
					fullName
				}
			}
			images {
				id
				sessionId
				title
				image
				thumbnail
				contentType
				width
				height
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			videos {
				id
				sessionId
				title
				video
				thumbnail
				contentType
				width
				height
				durationSeconds
				fileSizeBytes
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			completedTasks {
				id
				sessionId
				taskId
				completedById
				completedAt
				notes
				task {
					id
					scopeDescription
					sessionDescription
					frequency
					order
				}
				completedBy {
					id
					fullName
				}
			}
		}
	}
`;

// ==================== PROJECT SESSION QUERIES ====================

export const PROJECT_SESSION_QUERY = gql`
	query ProjectSession($id: UUID!) {
		projectSession(id: $id) {
			id
			projectId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			end
			date
			createdById
			closedById
			createdAt
			updatedAt
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			closedBy {
				id
				fullName
			}
			notes {
				id
				sessionId
				content
				authorId
				internal
				createdAt
				updatedAt
				author {
					id
					fullName
				}
			}
			images {
				id
				sessionId
				title
				image
				thumbnail
				contentType
				width
				height
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			videos {
				id
				sessionId
				title
				video
				thumbnail
				contentType
				width
				height
				durationSeconds
				fileSizeBytes
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			completedTasks {
				id
				sessionId
				taskId
				completedById
				completedAt
				notes
				task {
					id
					scopeDescription
					sessionDescription
					order
				}
				completedBy {
					id
					fullName
				}
			}
		}
	}
`;

export const PROJECT_SESSIONS_QUERY = gql`
	query ProjectSessions($projectId: UUID!) {
		projectSessions(projectId: $projectId) {
			id
			projectId
			start
			end
			date
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			closedBy {
				id
				fullName
			}
		}
	}
`;

export const LATEST_PROJECT_SESSION_QUERY = gql`
	query LatestProjectSession($projectId: UUID!) {
		latestProjectSession(projectId: $projectId) {
			id
			projectId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			end
			date
			createdById
			closedById
			createdAt
			updatedAt
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			closedBy {
				id
				fullName
			}
			notes {
				id
				sessionId
				content
				authorId
				internal
				createdAt
				updatedAt
				author {
					id
					fullName
				}
			}
			images {
				id
				sessionId
				title
				image
				thumbnail
				contentType
				width
				height
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			videos {
				id
				sessionId
				title
				video
				thumbnail
				contentType
				width
				height
				durationSeconds
				fileSizeBytes
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			completedTasks {
				id
				sessionId
				taskId
				completedById
				completedAt
				notes
				task {
					id
					scopeDescription
					sessionDescription
					order
				}
				completedBy {
					id
					fullName
				}
			}
		}
	}
`;

export const ACTIVE_PROJECT_SESSION_QUERY = gql`
	query ActiveProjectSession($projectId: UUID!) {
		activeProjectSession(projectId: $projectId) {
			id
			projectId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			end
			date
			createdById
			closedById
			createdAt
			updatedAt
			isActive
			durationSeconds
			createdBy {
				id
				fullName
			}
			notes {
				id
				sessionId
				content
				authorId
				internal
				createdAt
				updatedAt
				author {
					id
					fullName
				}
			}
			images {
				id
				sessionId
				title
				image
				thumbnail
				contentType
				width
				height
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			videos {
				id
				sessionId
				title
				video
				thumbnail
				contentType
				width
				height
				durationSeconds
				fileSizeBytes
				uploadedById
				notes
				internal
				createdAt
				uploadedBy {
					id
					fullName
				}
			}
			completedTasks {
				id
				sessionId
				taskId
				completedById
				completedAt
				notes
				task {
					id
					scopeDescription
					sessionDescription
					order
				}
				completedBy {
					id
					fullName
				}
			}
		}
	}
`;

// ==================== TYPE DEFINITIONS ====================

export interface TeamProfileRef {
	id: string;
	fullName: string;
}

export interface SessionNote {
	id: string;
	sessionId: string;
	content: string;
	authorId: string;
	internal: boolean;
	createdAt: string;
	updatedAt: string;
	author: TeamProfileRef | null;
}

export interface SessionImage {
	id: string;
	sessionId: string;
	title: string | null;
	image: string;
	thumbnail: string | null;
	contentType: string;
	width: number | null;
	height: number | null;
	uploadedById: string;
	notes: string | null;
	internal: boolean;
	createdAt: string;
	uploadedBy: TeamProfileRef | null;
}

export interface SessionVideo {
	id: string;
	sessionId: string;
	title: string | null;
	video: string;
	thumbnail: string | null;
	contentType: string;
	width: number | null;
	height: number | null;
	durationSeconds: number | null;
	fileSizeBytes: number | null;
	uploadedById: string;
	notes: string | null;
	internal: boolean;
	createdAt: string;
	uploadedBy: TeamProfileRef | null;
}

export interface ServiceScopeTask {
	id: string;
	scopeDescription: string;
	sessionDescription: string;
	frequency: string;
	order: number;
}

export interface ProjectScopeTask {
	id: string;
	scopeDescription: string;
	sessionDescription: string;
	order: number;
}

export interface ServiceTaskCompletion {
	id: string;
	sessionId: string;
	taskId: string;
	completedById: string;
	completedAt: string;
	notes: string | null;
	task: ServiceScopeTask | null;
	completedBy: TeamProfileRef | null;
}

export interface ProjectTaskCompletion {
	id: string;
	sessionId: string;
	taskId: string;
	completedById: string;
	completedAt: string;
	notes: string | null;
	task: ProjectScopeTask | null;
	completedBy: TeamProfileRef | null;
}

export interface ServiceSession {
	id: string;
	serviceId: string;
	accountId: string;
	accountAddressId: string;
	customerId: string;
	scopeId: string | null;
	start: string;
	end: string | null;
	date: string;
	createdById: string;
	closedById: string | null;
	createdAt: string;
	updatedAt: string;
	isActive: boolean;
	durationSeconds: number | null;
	createdBy: TeamProfileRef | null;
	closedBy: TeamProfileRef | null;
	notes: SessionNote[];
	images: SessionImage[];
	videos: SessionVideo[];
	completedTasks: ServiceTaskCompletion[];
}

export interface ServiceSessionSummary {
	id: string;
	serviceId: string;
	start: string;
	end: string | null;
	date: string;
	isActive: boolean;
	durationSeconds: number | null;
	createdBy: TeamProfileRef | null;
	closedBy: TeamProfileRef | null;
}

export interface ProjectSession {
	id: string;
	projectId: string;
	accountId: string | null;
	accountAddressId: string | null;
	customerId: string;
	scopeId: string | null;
	start: string;
	end: string | null;
	date: string;
	createdById: string;
	closedById: string | null;
	createdAt: string;
	updatedAt: string;
	isActive: boolean;
	durationSeconds: number | null;
	createdBy: TeamProfileRef | null;
	closedBy: TeamProfileRef | null;
	notes: SessionNote[];
	images: SessionImage[];
	videos: SessionVideo[];
	completedTasks: ProjectTaskCompletion[];
}

export interface ProjectSessionSummary {
	id: string;
	projectId: string;
	start: string;
	end: string | null;
	date: string;
	isActive: boolean;
	durationSeconds: number | null;
	createdBy: TeamProfileRef | null;
	closedBy: TeamProfileRef | null;
}

// Query result types
export interface ServiceSessionQueryResult {
	serviceSession: ServiceSession | null;
}

export interface ServiceSessionsQueryResult {
	serviceSessions: ServiceSessionSummary[];
}

export interface ActiveServiceSessionQueryResult {
	activeServiceSession: ServiceSession | null;
}

export interface LatestServiceSessionQueryResult {
	latestServiceSession: ServiceSession | null;
}

export interface ProjectSessionQueryResult {
	projectSession: ProjectSession | null;
}

export interface ProjectSessionsQueryResult {
	projectSessions: ProjectSessionSummary[];
}

export interface ActiveProjectSessionQueryResult {
	activeProjectSession: ProjectSession | null;
}

export interface LatestProjectSessionQueryResult {
	latestProjectSession: ProjectSession | null;
}
