/**
 * TypeScript type definitions for the Trackex MCP Server
 */

// MCP Protocol Types
export interface MCPRequest {
	jsonrpc: '2.0';
	id?: string | number | null;
	method: string;
	params?: any;
}

export interface MCPResponse {
	jsonrpc: '2.0';
	id: string | number | null;
	result?: any;
	error?: MCPError;
}

export interface MCPError {
	code: number;
	message: string;
	data?: any;
}

export interface MCPNotification {
	jsonrpc: '2.0';
	method: string;
	params?: any;
}

// MCP Tool Types
export interface MCPTool {
	name: string;
	description: string;
	inputSchema: {
		type: 'object';
		properties: Record<string, any>;
		required?: string[];
	};
}

export interface MCPToolCall {
	name: string;
	arguments: Record<string, any>;
}

export interface MCPToolResult {
	content: Array<{
		type: 'text';
		text: string;
	}>;
	isError?: boolean;
}

// Authentication Types
export interface OAuthState {
	state: string;
	codeVerifier: string;
	redirectUri: string;
	timestamp: number;
}

export interface OAuthTokenResponse {
	access_token: string;
	token_type: string;
	expires_in: number;
	refresh_token?: string;
	scope: string;
}

export interface MCPSession {
	sessionId: string;
	userId: string;
	trackexToken: string;
	expiresAt: number;
	userInfo: {
		email: string;
		name: string;
		tenantId: string;
	};
}

// Trackex API Types
export interface TrackexTask {
	id: number;
	title: string;
	description?: string;
	priority: 'low' | 'medium' | 'high';
	status: string;
	due_date?: string;
	created_at: string;
	updated_at: string;
	assigned_to?: number;
	created_by: number;
	is_adhoc?: boolean;
	is_pending?: boolean;
}

export interface TrackexTaskCreateRequest {
	title: string;
	description?: string;
	priority?: 'low' | 'medium' | 'high';
	due_date?: string;
}

export interface TrackexAPIResponse<T = any> {
	success: boolean;
	data?: T;
	message?: string;
	error?: string;
}

export interface TrackexUser {
	id: number;
	email: string;
	name: string;
	role: string;
}

// Error Types
export enum MCPErrorCode {
	ParseError = -32700,
	InvalidRequest = -32600,
	MethodNotFound = -32601,
	InvalidParams = -32602,
	InternalError = -32603,
	ServerError = -32000,
	AuthenticationError = -32001,
	AuthorizationError = -32002,
	RateLimitError = -32003,
}

// SSE Types
export interface SSEMessage {
	id?: string;
	event?: string;
	data: string;
	retry?: number;
}

// Configuration Types
export interface MCPServerConfig {
	name: string;
	version: string;
	capabilities: {
		tools?: {
			listChanged?: boolean;
		};
		logging?: {};
		prompts?: {
			listChanged?: boolean;
		};
		resources?: {
			subscribe?: boolean;
			listChanged?: boolean;
		};
	};
	protocolVersion: string;
}

export interface AuthContext {
	isAuthenticated: boolean;
	userId?: string;
	trackexToken?: string;
	userInfo?: {
		email: string;
		name: string;
		tenantId: string;
	};
} 