import {
	GetScopeTemplatesStore,
	GetProjectScopeTemplatesStore,
	// Scope Template stores
	CreateScopeTemplateStore,
	UpdateScopeTemplateStore,
	DeleteScopeTemplateStore,
	CreateAreaTemplateStore,
	UpdateAreaTemplateStore,
	DeleteAreaTemplateStore,
	CreateTaskTemplateStore,
	UpdateTaskTemplateStore,
	DeleteTaskTemplateStore,
	CreateScopeTemplateFromJsonStore,
	CreateScopeFromTemplateStore,
	// Project Scope Template stores
	CreateProjectScopeTemplateStore,
	UpdateProjectScopeTemplateStore,
	DeleteProjectScopeTemplateStore,
	CreateProjectAreaTemplateStore,
	UpdateProjectAreaTemplateStore,
	DeleteProjectAreaTemplateStore,
	CreateProjectTaskTemplateStore,
	UpdateProjectTaskTemplateStore,
	DeleteProjectTaskTemplateStore,
	CreateProjectScopeTemplateFromJsonStore,
	CreateProjectScopeFromTemplateStore,
	// Service Scope stores (for active scopes on accounts)
	CreateScopeStore,
	UpdateScopeStore,
	DeleteScopeStore,
	CreateAreaStore,
	UpdateAreaStore,
	DeleteAreaStore,
	CreateTaskStore,
	UpdateTaskStore,
	DeleteTaskStore,
	// Project Scope stores (for active scopes on projects)
	CreateProjectScopeStore,
	UpdateProjectScopeStore,
	DeleteProjectScopeStore,
	CreateProjectScopeCategoryStore,
	UpdateProjectScopeCategoryStore,
	DeleteProjectScopeCategoryStore,
	CreateProjectScopeTaskStore,
	UpdateProjectScopeTaskStore,
	DeleteProjectScopeTaskStore
} from '$houdini';
import type {
	// Scope Template types
	CreateScopeTemplate$input,
	CreateScopeTemplate$result,
	UpdateScopeTemplate$input,
	UpdateScopeTemplate$result,
	CreateAreaTemplate$input,
	CreateAreaTemplate$result,
	UpdateAreaTemplate$input,
	UpdateAreaTemplate$result,
	CreateTaskTemplate$input,
	CreateTaskTemplate$result,
	UpdateTaskTemplate$input,
	UpdateTaskTemplate$result,
	CreateScopeTemplateFromJson$result,
	CreateScopeFromTemplate$input,
	CreateScopeFromTemplate$result,
	// Project Scope Template types
	CreateProjectScopeTemplate$input,
	CreateProjectScopeTemplate$result,
	UpdateProjectScopeTemplate$input,
	UpdateProjectScopeTemplate$result,
	CreateProjectAreaTemplate$input,
	CreateProjectAreaTemplate$result,
	UpdateProjectAreaTemplate$input,
	UpdateProjectAreaTemplate$result,
	CreateProjectTaskTemplate$input,
	CreateProjectTaskTemplate$result,
	UpdateProjectTaskTemplate$input,
	UpdateProjectTaskTemplate$result,
	CreateProjectScopeTemplateFromJson$result,
	CreateProjectScopeFromTemplate$input,
	CreateProjectScopeFromTemplate$result,
	// Service Scope types (active scopes on accounts)
	CreateScope$input,
	CreateScope$result,
	UpdateScope$input,
	UpdateScope$result,
	CreateArea$input,
	CreateArea$result,
	UpdateArea$input,
	UpdateArea$result,
	CreateTask$input,
	CreateTask$result,
	UpdateTask$input,
	UpdateTask$result,
	// Project Scope types (active scopes on projects)
	CreateProjectScope$input,
	CreateProjectScope$result,
	UpdateProjectScope$input,
	UpdateProjectScope$result,
	CreateProjectScopeCategory$input,
	CreateProjectScopeCategory$result,
	UpdateProjectScopeCategory$input,
	UpdateProjectScopeCategory$result,
	CreateProjectScopeTask$input,
	CreateProjectScopeTask$result,
	UpdateProjectScopeTask$input,
	UpdateProjectScopeTask$result
} from '$houdini';

// Re-export stores for server-side use
export { GetScopeTemplatesStore, GetProjectScopeTemplatesStore };

// ============================================================================
// Service Scope Template Functions
// ============================================================================

export type ScopeTemplateInput = CreateScopeTemplate$input['input'];
export type ScopeTemplate = CreateScopeTemplate$result['createScopeTemplate'];

export async function createScopeTemplate(input: ScopeTemplateInput): Promise<ScopeTemplate> {
	const store = new CreateScopeTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create scope template');
	}

	const result = data?.createScopeTemplate;
	if (!result) {
		throw new Error('No scope template returned by the server');
	}

	return result;
}

export type ScopeTemplateUpdateInput = UpdateScopeTemplate$input['input'];
export type UpdatedScopeTemplate = UpdateScopeTemplate$result['updateScopeTemplate'];

export async function updateScopeTemplate(
	input: ScopeTemplateUpdateInput
): Promise<UpdatedScopeTemplate> {
	const store = new UpdateScopeTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update scope template');
	}

	const result = data?.updateScopeTemplate;
	if (!result) {
		throw new Error('No scope template returned by the server');
	}

	return result;
}

export async function deleteScopeTemplate(id: string): Promise<void> {
	const store = new DeleteScopeTemplateStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete scope template');
	}

	if (!data?.deleteScopeTemplate) {
		throw new Error('Failed to delete scope template');
	}
}

// ============================================================================
// Service Area Template Functions
// ============================================================================

export type AreaTemplateInput = CreateAreaTemplate$input['input'];
export type AreaTemplate = CreateAreaTemplate$result['createAreaTemplate'];

export async function createAreaTemplate(input: AreaTemplateInput): Promise<AreaTemplate> {
	const store = new CreateAreaTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create area template');
	}

	const result = data?.createAreaTemplate;
	if (!result) {
		throw new Error('No area template returned by the server');
	}

	return result;
}

export type AreaTemplateUpdateInput = UpdateAreaTemplate$input['input'];
export type UpdatedAreaTemplate = UpdateAreaTemplate$result['updateAreaTemplate'];

export async function updateAreaTemplate(
	input: AreaTemplateUpdateInput
): Promise<UpdatedAreaTemplate> {
	const store = new UpdateAreaTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update area template');
	}

	const result = data?.updateAreaTemplate;
	if (!result) {
		throw new Error('No area template returned by the server');
	}

	return result;
}

export async function deleteAreaTemplate(id: string): Promise<void> {
	const store = new DeleteAreaTemplateStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete area template');
	}

	if (!data?.deleteAreaTemplate) {
		throw new Error('Failed to delete area template');
	}
}

// ============================================================================
// Service Task Template Functions
// ============================================================================

export type TaskTemplateInput = CreateTaskTemplate$input['input'];
export type TaskTemplate = CreateTaskTemplate$result['createTaskTemplate'];

export async function createTaskTemplate(input: TaskTemplateInput): Promise<TaskTemplate> {
	const store = new CreateTaskTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create task template');
	}

	const result = data?.createTaskTemplate;
	if (!result) {
		throw new Error('No task template returned by the server');
	}

	return result;
}

export type TaskTemplateUpdateInput = UpdateTaskTemplate$input['input'];
export type UpdatedTaskTemplate = UpdateTaskTemplate$result['updateTaskTemplate'];

export async function updateTaskTemplate(
	input: TaskTemplateUpdateInput
): Promise<UpdatedTaskTemplate> {
	const store = new UpdateTaskTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update task template');
	}

	const result = data?.updateTaskTemplate;
	if (!result) {
		throw new Error('No task template returned by the server');
	}

	return result;
}

export async function deleteTaskTemplate(id: string): Promise<void> {
	const store = new DeleteTaskTemplateStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete task template');
	}

	if (!data?.deleteTaskTemplate) {
		throw new Error('Failed to delete task template');
	}
}

// ============================================================================
// Service Scope Template from JSON
// ============================================================================

export type ScopeTemplateFromJson =
	CreateScopeTemplateFromJson$result['createScopeTemplateFromJson'];

export async function createScopeTemplateFromJson(
	payload: unknown,
	replace: boolean = false
): Promise<ScopeTemplateFromJson> {
	const store = new CreateScopeTemplateFromJsonStore();
	const { data, errors } = await store.mutate({ payload, replace });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create scope template from JSON');
	}

	const result = data?.createScopeTemplateFromJson;
	if (!result) {
		throw new Error('No scope template returned by the server');
	}

	return result;
}

// ============================================================================
// Project Scope Template Functions
// ============================================================================

export type ProjectScopeTemplateInput = CreateProjectScopeTemplate$input['input'];
export type ProjectScopeTemplate = CreateProjectScopeTemplate$result['createProjectScopeTemplate'];

export async function createProjectScopeTemplate(
	input: ProjectScopeTemplateInput
): Promise<ProjectScopeTemplate> {
	const store = new CreateProjectScopeTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project scope template');
	}

	const result = data?.createProjectScopeTemplate;
	if (!result) {
		throw new Error('No project scope template returned by the server');
	}

	return result;
}

export type ProjectScopeTemplateUpdateInput = UpdateProjectScopeTemplate$input['input'];
export type UpdatedProjectScopeTemplate =
	UpdateProjectScopeTemplate$result['updateProjectScopeTemplate'];

export async function updateProjectScopeTemplate(
	input: ProjectScopeTemplateUpdateInput
): Promise<UpdatedProjectScopeTemplate> {
	const store = new UpdateProjectScopeTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update project scope template');
	}

	const result = data?.updateProjectScopeTemplate;
	if (!result) {
		throw new Error('No project scope template returned by the server');
	}

	return result;
}

export async function deleteProjectScopeTemplate(id: string): Promise<void> {
	const store = new DeleteProjectScopeTemplateStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete project scope template');
	}

	if (!data?.deleteProjectScopeTemplate) {
		throw new Error('Failed to delete project scope template');
	}
}

// ============================================================================
// Project Category (Area) Template Functions
// ============================================================================

export type ProjectAreaTemplateInput = CreateProjectAreaTemplate$input['input'];
export type ProjectAreaTemplate = CreateProjectAreaTemplate$result['createProjectAreaTemplate'];

export async function createProjectAreaTemplate(
	input: ProjectAreaTemplateInput
): Promise<ProjectAreaTemplate> {
	const store = new CreateProjectAreaTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project category template');
	}

	const result = data?.createProjectAreaTemplate;
	if (!result) {
		throw new Error('No project category template returned by the server');
	}

	return result;
}

export type ProjectAreaTemplateUpdateInput = UpdateProjectAreaTemplate$input['input'];
export type UpdatedProjectAreaTemplate =
	UpdateProjectAreaTemplate$result['updateProjectAreaTemplate'];

export async function updateProjectAreaTemplate(
	input: ProjectAreaTemplateUpdateInput
): Promise<UpdatedProjectAreaTemplate> {
	const store = new UpdateProjectAreaTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update project category template');
	}

	const result = data?.updateProjectAreaTemplate;
	if (!result) {
		throw new Error('No project category template returned by the server');
	}

	return result;
}

export async function deleteProjectAreaTemplate(id: string): Promise<void> {
	const store = new DeleteProjectAreaTemplateStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete project category template');
	}

	if (!data?.deleteProjectAreaTemplate) {
		throw new Error('Failed to delete project category template');
	}
}

// ============================================================================
// Project Task Template Functions
// ============================================================================

export type ProjectTaskTemplateInput = CreateProjectTaskTemplate$input['input'];
export type ProjectTaskTemplate = CreateProjectTaskTemplate$result['createProjectTaskTemplate'];

export async function createProjectTaskTemplate(
	input: ProjectTaskTemplateInput
): Promise<ProjectTaskTemplate> {
	const store = new CreateProjectTaskTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project task template');
	}

	const result = data?.createProjectTaskTemplate;
	if (!result) {
		throw new Error('No project task template returned by the server');
	}

	return result;
}

export type ProjectTaskTemplateUpdateInput = UpdateProjectTaskTemplate$input['input'];
export type UpdatedProjectTaskTemplate =
	UpdateProjectTaskTemplate$result['updateProjectTaskTemplate'];

export async function updateProjectTaskTemplate(
	input: ProjectTaskTemplateUpdateInput
): Promise<UpdatedProjectTaskTemplate> {
	const store = new UpdateProjectTaskTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update project task template');
	}

	const result = data?.updateProjectTaskTemplate;
	if (!result) {
		throw new Error('No project task template returned by the server');
	}

	return result;
}

export async function deleteProjectTaskTemplate(id: string): Promise<void> {
	const store = new DeleteProjectTaskTemplateStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete project task template');
	}

	if (!data?.deleteProjectTaskTemplate) {
		throw new Error('Failed to delete project task template');
	}
}

// ============================================================================
// Project Scope Template from JSON
// ============================================================================

export type ProjectScopeTemplateFromJson =
	CreateProjectScopeTemplateFromJson$result['createProjectScopeTemplateFromJson'];

export async function createProjectScopeTemplateFromJson(
	payload: unknown,
	replace: boolean = false
): Promise<ProjectScopeTemplateFromJson> {
	const store = new CreateProjectScopeTemplateFromJsonStore();
	const { data, errors } = await store.mutate({ payload, replace });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project scope template from JSON');
	}

	const result = data?.createProjectScopeTemplateFromJson;
	if (!result) {
		throw new Error('No project scope template returned by the server');
	}

	return result;
}

// ============================================================================
// Create Scope from Template (Instantiation)
// ============================================================================

export type CreateScopeFromTemplateInput = CreateScopeFromTemplate$input['input'];
export type CreatedScopeFromTemplate = CreateScopeFromTemplate$result['createScopeFromTemplate'];

export async function createScopeFromTemplate(
	input: CreateScopeFromTemplateInput
): Promise<CreatedScopeFromTemplate> {
	const store = new CreateScopeFromTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create scope from template');
	}

	const result = data?.createScopeFromTemplate;
	if (!result) {
		throw new Error('No scope returned by the server');
	}

	return result;
}

// ============================================================================
// Create Project Scope from Template (Instantiation)
// ============================================================================

export type CreateProjectScopeFromTemplateInput = CreateProjectScopeFromTemplate$input['input'];
export type CreatedProjectScopeFromTemplate =
	CreateProjectScopeFromTemplate$result['createProjectScopeFromTemplate'];

export async function createProjectScopeFromTemplate(
	input: CreateProjectScopeFromTemplateInput
): Promise<CreatedProjectScopeFromTemplate> {
	const store = new CreateProjectScopeFromTemplateStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project scope from template');
	}

	const result = data?.createProjectScopeFromTemplate;
	if (!result) {
		throw new Error('No project scope returned by the server');
	}

	return result;
}

// ============================================================================
// Service Scope Functions (Active Scopes on Accounts)
// ============================================================================

export type ServiceScopeInput = CreateScope$input['input'];
export type ServiceScope = CreateScope$result['createScope'];

export async function createScope(input: ServiceScopeInput): Promise<ServiceScope> {
	const store = new CreateScopeStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create scope');
	}

	const result = data?.createScope;
	if (!result) {
		throw new Error('No scope returned by the server');
	}

	return result;
}

export type ServiceScopeUpdateInput = UpdateScope$input['input'];
export type UpdatedServiceScope = UpdateScope$result['updateScope'];

export async function updateScope(input: ServiceScopeUpdateInput): Promise<UpdatedServiceScope> {
	const store = new UpdateScopeStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update scope');
	}

	const result = data?.updateScope;
	if (!result) {
		throw new Error('No scope returned by the server');
	}

	return result;
}

export async function deleteScope(id: string): Promise<void> {
	const store = new DeleteScopeStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete scope');
	}

	if (!data?.deleteScope) {
		throw new Error('Failed to delete scope');
	}
}

// ============================================================================
// Service Area Functions (Areas within Active Scopes)
// ============================================================================

export type ServiceAreaInput = CreateArea$input['input'];
export type ServiceArea = CreateArea$result['createArea'];

export async function createArea(input: ServiceAreaInput): Promise<ServiceArea> {
	const store = new CreateAreaStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create area');
	}

	const result = data?.createArea;
	if (!result) {
		throw new Error('No area returned by the server');
	}

	return result;
}

export type ServiceAreaUpdateInput = UpdateArea$input['input'];
export type UpdatedServiceArea = UpdateArea$result['updateArea'];

export async function updateArea(input: ServiceAreaUpdateInput): Promise<UpdatedServiceArea> {
	const store = new UpdateAreaStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update area');
	}

	const result = data?.updateArea;
	if (!result) {
		throw new Error('No area returned by the server');
	}

	return result;
}

export async function deleteArea(id: string): Promise<void> {
	const store = new DeleteAreaStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete area');
	}

	if (!data?.deleteArea) {
		throw new Error('Failed to delete area');
	}
}

// ============================================================================
// Service Task Functions (Tasks within Active Scopes)
// ============================================================================

export type ServiceTaskInput = CreateTask$input['input'];
export type ServiceTask = CreateTask$result['createTask'];

export async function createTask(input: ServiceTaskInput): Promise<ServiceTask> {
	const store = new CreateTaskStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create task');
	}

	const result = data?.createTask;
	if (!result) {
		throw new Error('No task returned by the server');
	}

	return result;
}

export type ServiceTaskUpdateInput = UpdateTask$input['input'];
export type UpdatedServiceTask = UpdateTask$result['updateTask'];

export async function updateTask(input: ServiceTaskUpdateInput): Promise<UpdatedServiceTask> {
	const store = new UpdateTaskStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update task');
	}

	const result = data?.updateTask;
	if (!result) {
		throw new Error('No task returned by the server');
	}

	return result;
}

export async function deleteTask(id: string): Promise<void> {
	const store = new DeleteTaskStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete task');
	}

	if (!data?.deleteTask) {
		throw new Error('Failed to delete task');
	}
}

// ============================================================================
// Project Scope Functions (Active Scopes on Projects)
// ============================================================================

export type ProjectScopeInput = CreateProjectScope$input['input'];
export type ProjectScope = CreateProjectScope$result['createProjectScope'];

export async function createProjectScope(input: ProjectScopeInput): Promise<ProjectScope> {
	const store = new CreateProjectScopeStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project scope');
	}

	const result = data?.createProjectScope;
	if (!result) {
		throw new Error('No project scope returned by the server');
	}

	return result;
}

export type ProjectScopeUpdateInput = UpdateProjectScope$input['input'];
export type UpdatedProjectScope = UpdateProjectScope$result['updateProjectScope'];

export async function updateProjectScope(
	input: ProjectScopeUpdateInput
): Promise<UpdatedProjectScope> {
	const store = new UpdateProjectScopeStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update project scope');
	}

	const result = data?.updateProjectScope;
	if (!result) {
		throw new Error('No project scope returned by the server');
	}

	return result;
}

export async function deleteProjectScope(id: string): Promise<void> {
	const store = new DeleteProjectScopeStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete project scope');
	}

	if (!data?.deleteProjectScope) {
		throw new Error('Failed to delete project scope');
	}
}

// ============================================================================
// Project Scope Category Functions (Categories within Active Project Scopes)
// ============================================================================

export type ProjectScopeCategoryInput = CreateProjectScopeCategory$input['input'];
export type ProjectScopeCategory = CreateProjectScopeCategory$result['createProjectScopeCategory'];

export async function createProjectScopeCategory(
	input: ProjectScopeCategoryInput
): Promise<ProjectScopeCategory> {
	const store = new CreateProjectScopeCategoryStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project scope category');
	}

	const result = data?.createProjectScopeCategory;
	if (!result) {
		throw new Error('No project scope category returned by the server');
	}

	return result;
}

export type ProjectScopeCategoryUpdateInput = UpdateProjectScopeCategory$input['input'];
export type UpdatedProjectScopeCategory =
	UpdateProjectScopeCategory$result['updateProjectScopeCategory'];

export async function updateProjectScopeCategory(
	input: ProjectScopeCategoryUpdateInput
): Promise<UpdatedProjectScopeCategory> {
	const store = new UpdateProjectScopeCategoryStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update project scope category');
	}

	const result = data?.updateProjectScopeCategory;
	if (!result) {
		throw new Error('No project scope category returned by the server');
	}

	return result;
}

export async function deleteProjectScopeCategory(id: string): Promise<void> {
	const store = new DeleteProjectScopeCategoryStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete project scope category');
	}

	if (!data?.deleteProjectScopeCategory) {
		throw new Error('Failed to delete project scope category');
	}
}

// ============================================================================
// Project Scope Task Functions (Tasks within Active Project Scopes)
// ============================================================================

export type ProjectScopeTaskInput = CreateProjectScopeTask$input['input'];
export type ProjectScopeTask = CreateProjectScopeTask$result['createProjectScopeTask'];

export async function createProjectScopeTask(
	input: ProjectScopeTaskInput
): Promise<ProjectScopeTask> {
	const store = new CreateProjectScopeTaskStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to create project scope task');
	}

	const result = data?.createProjectScopeTask;
	if (!result) {
		throw new Error('No project scope task returned by the server');
	}

	return result;
}

export type ProjectScopeTaskUpdateInput = UpdateProjectScopeTask$input['input'];
export type UpdatedProjectScopeTask = UpdateProjectScopeTask$result['updateProjectScopeTask'];

export async function updateProjectScopeTask(
	input: ProjectScopeTaskUpdateInput
): Promise<UpdatedProjectScopeTask> {
	const store = new UpdateProjectScopeTaskStore();
	const { data, errors } = await store.mutate({ input });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to update project scope task');
	}

	const result = data?.updateProjectScopeTask;
	if (!result) {
		throw new Error('No project scope task returned by the server');
	}

	return result;
}

export async function deleteProjectScopeTask(id: string): Promise<void> {
	const store = new DeleteProjectScopeTaskStore();
	const { data, errors } = await store.mutate({ id });

	if (errors?.length) {
		const message = errors.map((e) => e.message || 'Unknown error').join('; ');
		throw new Error(message || 'Failed to delete project scope task');
	}

	if (!data?.deleteProjectScopeTask) {
		throw new Error('Failed to delete project scope task');
	}
}
