type TRPCErrorCode =
	| "NOT_FOUND"
	| "UNAUTHORIZED"
	| "FORBIDDEN"
	| "INTERNAL_SERVER_ERROR"
	| "BAD_REQUEST"
	| "UNKNOWN";

export interface TRPCErrorInfo {
	code: TRPCErrorCode;
	title: string;
	message: string;
}

/**
 * Parse TRPC error and return structured error information with user-friendly messages
 */
export function parseTRPCError(error: unknown): TRPCErrorInfo {
	// Default fallback
	const defaultError: TRPCErrorInfo = {
		code: "UNKNOWN",
		title: "Error",
		message: "Something went wrong. Please try again later.",
	};

	if (!error) {
		return defaultError;
	}

	// Extract error message
	const errorMessage =
		typeof error === "object" && error !== null && "message" in error
			? String(error.message)
			: "";

	// Detect error code from message
	const upperMessage = errorMessage.toUpperCase();

	// NOT_FOUND errors
	if (
		upperMessage.includes("NOT_FOUND") ||
		upperMessage.includes("NOT FOUND") ||
		errorMessage.toLowerCase().includes("not found") ||
		errorMessage.toLowerCase().includes("doesn't exist") ||
		errorMessage.toLowerCase().includes("does not exist")
	) {
		return {
			code: "NOT_FOUND",
			title: "Not Found",
			message: errorMessage || "The requested resource was not found.",
		};
	}

	// UNAUTHORIZED errors
	if (
		upperMessage.includes("UNAUTHORIZED") ||
		upperMessage.includes("AUTHENTICATION REQUIRED") ||
		errorMessage.toLowerCase().includes("log in") ||
		errorMessage.toLowerCase().includes("login") ||
		errorMessage.toLowerCase().includes("not authenticated")
	) {
		return {
			code: "UNAUTHORIZED",
			title: "Authentication Required",
			message:
				errorMessage || "Please log in to continue accessing this resource.",
		};
	}

	// FORBIDDEN errors
	if (
		upperMessage.includes("FORBIDDEN") ||
		upperMessage.includes("ACCESS DENIED") ||
		errorMessage.toLowerCase().includes("permission") ||
		errorMessage.toLowerCase().includes("not allowed")
	) {
		return {
			code: "FORBIDDEN",
			title: "Access Denied",
			message:
				errorMessage || "You don't have permission to access this resource.",
		};
	}

	// INTERNAL_SERVER_ERROR
	if (
		upperMessage.includes("INTERNAL_SERVER_ERROR") ||
		upperMessage.includes("SERVER ERROR") ||
		errorMessage.toLowerCase().includes("internal error")
	) {
		return {
			code: "INTERNAL_SERVER_ERROR",
			title: "Server Error",
			message:
				errorMessage ||
				"An unexpected server error occurred. Please try again later.",
		};
	}

	// BAD_REQUEST errors
	if (
		upperMessage.includes("BAD_REQUEST") ||
		upperMessage.includes("INVALID") ||
		errorMessage.toLowerCase().includes("validation")
	) {
		return {
			code: "BAD_REQUEST",
			title: "Invalid Request",
			message: errorMessage || "The request contains invalid data.",
		};
	}

	// Return error with message if we couldn't categorize it
	return {
		code: "UNKNOWN",
		title: "Error",
		message: errorMessage || defaultError.message,
	};
}

/**
 * Check if error is a NOT_FOUND error
 */
export function isTRPCNotFoundError(error: unknown): boolean {
	const errorInfo = parseTRPCError(error);
	return errorInfo.code === "NOT_FOUND";
}

/**
 * Check if error is an authentication error (UNAUTHORIZED)
 */
export function isTRPCAuthError(error: unknown): boolean {
	const errorInfo = parseTRPCError(error);
	return errorInfo.code === "UNAUTHORIZED";
}

/**
 * Check if error is a permission error (FORBIDDEN)
 */
export function isTRPCPermissionError(error: unknown): boolean {
	const errorInfo = parseTRPCError(error);
	return errorInfo.code === "FORBIDDEN";
}
