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

// ==================== SESSION LIFECYCLE ====================

export const OPEN_SERVICE_SESSION = gql`
	mutation OpenServiceSession($input: OpenServiceSessionInput!) {
		openServiceSession(input: $input) {
			id
			serviceId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			date
			createdById
			isActive
			createdBy {
				id
				fullName
			}
		}
	}
`;

export const CLOSE_SERVICE_SESSION = gql`
	mutation CloseServiceSession($input: CloseServiceSessionInput!) {
		closeServiceSession(input: $input) {
			id
			serviceId
			start
			end
			date
			isActive
			durationSeconds
			closedById
			closedBy {
				id
				fullName
			}
		}
	}
`;

export const REVERT_SERVICE_SESSION = gql`
	mutation RevertServiceSession($serviceId: UUID!) {
		revertServiceSession(serviceId: $serviceId)
	}
`;

export const OPEN_PROJECT_SESSION = gql`
	mutation OpenProjectSession($input: OpenProjectSessionInput!) {
		openProjectSession(input: $input) {
			id
			projectId
			accountId
			accountAddressId
			customerId
			scopeId
			start
			date
			createdById
			isActive
			createdBy {
				id
				fullName
			}
		}
	}
`;

export const CLOSE_PROJECT_SESSION = gql`
	mutation CloseProjectSession($input: CloseProjectSessionInput!) {
		closeProjectSession(input: $input) {
			id
			projectId
			start
			end
			date
			isActive
			durationSeconds
			closedById
			closedBy {
				id
				fullName
			}
		}
	}
`;

export const REVERT_PROJECT_SESSION = gql`
	mutation RevertProjectSession($projectId: UUID!) {
		revertProjectSession(projectId: $projectId)
	}
`;

// ==================== TASK COMPLETIONS ====================

export const ADD_SERVICE_TASK_COMPLETION = gql`
	mutation AddServiceTaskCompletion($serviceId: UUID!, $taskId: UUID!, $notes: String) {
		addServiceTaskCompletion(serviceId: $serviceId, taskId: $taskId, notes: $notes) {
			id
			sessionId
			taskId
			completedById
			completedAt
			notes
			task {
				id
				scopeDescription
				sessionDescription
				frequency
				order
			}
			completedBy {
				id
				fullName
			}
		}
	}
`;

export const REMOVE_SERVICE_TASK_COMPLETION = gql`
	mutation RemoveServiceTaskCompletion($serviceId: UUID!, $taskId: UUID!) {
		removeServiceTaskCompletion(serviceId: $serviceId, taskId: $taskId)
	}
`;

export const ADD_PROJECT_TASK_COMPLETION = gql`
	mutation AddProjectTaskCompletion($projectId: UUID!, $taskId: UUID!, $notes: String) {
		addProjectTaskCompletion(projectId: $projectId, taskId: $taskId, notes: $notes) {
			id
			sessionId
			taskId
			completedById
			completedAt
			notes
			task {
				id
				scopeDescription
				sessionDescription
				order
			}
			completedBy {
				id
				fullName
			}
		}
	}
`;

export const REMOVE_PROJECT_TASK_COMPLETION = gql`
	mutation RemoveProjectTaskCompletion($projectId: UUID!, $taskId: UUID!) {
		removeProjectTaskCompletion(projectId: $projectId, taskId: $taskId)
	}
`;

// ==================== SERVICE SESSION NOTES ====================

export const CREATE_SERVICE_SESSION_NOTE = gql`
	mutation CreateServiceSessionNote($input: CreateSessionNoteInput!) {
		createServiceSessionNote(input: $input) {
			id
			sessionId
			content
			authorId
			internal
			createdAt
			updatedAt
			author {
				id
				fullName
			}
		}
	}
`;

export const UPDATE_SERVICE_SESSION_NOTE = gql`
	mutation UpdateServiceSessionNote($id: UUID!, $input: UpdateSessionNoteInput!) {
		updateServiceSessionNote(id: $id, input: $input) {
			id
			sessionId
			content
			authorId
			internal
			createdAt
			updatedAt
			author {
				id
				fullName
			}
		}
	}
`;

export const DELETE_SERVICE_SESSION_NOTE = gql`
	mutation DeleteServiceSessionNote($id: UUID!) {
		deleteServiceSessionNote(id: $id)
	}
`;

// ==================== PROJECT SESSION NOTES ====================

export const CREATE_PROJECT_SESSION_NOTE = gql`
	mutation CreateProjectSessionNote($input: CreateSessionNoteInput!) {
		createProjectSessionNote(input: $input) {
			id
			sessionId
			content
			authorId
			internal
			createdAt
			updatedAt
			author {
				id
				fullName
			}
		}
	}
`;

export const UPDATE_PROJECT_SESSION_NOTE = gql`
	mutation UpdateProjectSessionNote($id: UUID!, $input: UpdateSessionNoteInput!) {
		updateProjectSessionNote(id: $id, input: $input) {
			id
			sessionId
			content
			authorId
			internal
			createdAt
			updatedAt
			author {
				id
				fullName
			}
		}
	}
`;

export const DELETE_PROJECT_SESSION_NOTE = gql`
	mutation DeleteProjectSessionNote($id: UUID!) {
		deleteProjectSessionNote(id: $id)
	}
`;

// ==================== SERVICE SESSION IMAGES ====================

export const UPLOAD_SERVICE_SESSION_IMAGE = gql`
	mutation UploadServiceSessionImage(
		$sessionId: UUID!
		$file: Upload!
		$title: String
		$notes: String
		$internal: Boolean
	) {
		uploadServiceSessionImage(
			sessionId: $sessionId
			file: $file
			title: $title
			notes: $notes
			internal: $internal
		) {
			id
			sessionId
			title
			image
			thumbnail
			contentType
			width
			height
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const UPDATE_SERVICE_SESSION_IMAGE = gql`
	mutation UpdateServiceSessionImage($id: UUID!, $input: UpdateSessionMediaInput!) {
		updateServiceSessionImage(id: $id, input: $input) {
			id
			sessionId
			title
			image
			thumbnail
			contentType
			width
			height
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const DELETE_SERVICE_SESSION_IMAGE = gql`
	mutation DeleteServiceSessionImage($id: UUID!) {
		deleteServiceSessionImage(id: $id)
	}
`;

// ==================== PROJECT SESSION IMAGES ====================

export const UPLOAD_PROJECT_SESSION_IMAGE = gql`
	mutation UploadProjectSessionImage(
		$sessionId: UUID!
		$file: Upload!
		$title: String
		$notes: String
		$internal: Boolean
	) {
		uploadProjectSessionImage(
			sessionId: $sessionId
			file: $file
			title: $title
			notes: $notes
			internal: $internal
		) {
			id
			sessionId
			title
			image
			thumbnail
			contentType
			width
			height
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const UPDATE_PROJECT_SESSION_IMAGE = gql`
	mutation UpdateProjectSessionImage($id: UUID!, $input: UpdateSessionMediaInput!) {
		updateProjectSessionImage(id: $id, input: $input) {
			id
			sessionId
			title
			image
			thumbnail
			contentType
			width
			height
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const DELETE_PROJECT_SESSION_IMAGE = gql`
	mutation DeleteProjectSessionImage($id: UUID!) {
		deleteProjectSessionImage(id: $id)
	}
`;

// ==================== SERVICE SESSION VIDEOS ====================

export const UPLOAD_SERVICE_SESSION_VIDEO = gql`
	mutation UploadServiceSessionVideo(
		$sessionId: UUID!
		$file: Upload!
		$title: String
		$notes: String
		$internal: Boolean
	) {
		uploadServiceSessionVideo(
			sessionId: $sessionId
			file: $file
			title: $title
			notes: $notes
			internal: $internal
		) {
			id
			sessionId
			title
			video
			thumbnail
			contentType
			width
			height
			durationSeconds
			fileSizeBytes
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const UPDATE_SERVICE_SESSION_VIDEO = gql`
	mutation UpdateServiceSessionVideo($id: UUID!, $input: UpdateSessionMediaInput!) {
		updateServiceSessionVideo(id: $id, input: $input) {
			id
			sessionId
			title
			video
			thumbnail
			contentType
			width
			height
			durationSeconds
			fileSizeBytes
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const DELETE_SERVICE_SESSION_VIDEO = gql`
	mutation DeleteServiceSessionVideo($id: UUID!) {
		deleteServiceSessionVideo(id: $id)
	}
`;

// ==================== PROJECT SESSION VIDEOS ====================

export const UPLOAD_PROJECT_SESSION_VIDEO = gql`
	mutation UploadProjectSessionVideo(
		$sessionId: UUID!
		$file: Upload!
		$title: String
		$notes: String
		$internal: Boolean
	) {
		uploadProjectSessionVideo(
			sessionId: $sessionId
			file: $file
			title: $title
			notes: $notes
			internal: $internal
		) {
			id
			sessionId
			title
			video
			thumbnail
			contentType
			width
			height
			durationSeconds
			fileSizeBytes
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const UPDATE_PROJECT_SESSION_VIDEO = gql`
	mutation UpdateProjectSessionVideo($id: UUID!, $input: UpdateSessionMediaInput!) {
		updateProjectSessionVideo(id: $id, input: $input) {
			id
			sessionId
			title
			video
			thumbnail
			contentType
			width
			height
			durationSeconds
			fileSizeBytes
			uploadedById
			notes
			internal
			createdAt
			uploadedBy {
				id
				fullName
			}
		}
	}
`;

export const DELETE_PROJECT_SESSION_VIDEO = gql`
	mutation DeleteProjectSessionVideo($id: UUID!) {
		deleteProjectSessionVideo(id: $id)
	}
`;

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

export interface OpenServiceSessionInput {
	serviceId: string;
}

export interface CloseServiceSessionInput {
	serviceId: string;
	taskIds: string[];
}

export interface OpenProjectSessionInput {
	projectId: string;
}

export interface CloseProjectSessionInput {
	projectId: string;
	taskIds: string[];
}

export interface CreateSessionNoteInput {
	sessionId: string;
	content: string;
	internal?: boolean;
}

export interface UpdateSessionNoteInput {
	content?: string;
	internal?: boolean;
}

export interface UpdateSessionMediaInput {
	title?: string;
	notes?: string;
	internal?: boolean;
}
