import type { GatewayRequest, OutboundMessage } from '../protocol.js';
import { makeError, makeResponse, ErrorCode } from '../protocol.js';
import type { CanvasStore } from '../canvas-store.js';

export interface CanvasHandlerDeps {
  store: CanvasStore;
}

export function createCanvasHandlers(deps: CanvasHandlerDeps) {
  return {
    'canvas.list': async (request: GatewayRequest): Promise<OutboundMessage> => {
      const params = request.params as { sessionId?: string } | undefined;
      if (!params?.sessionId) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'sessionId is required');
      }

      const artifacts = deps.store.list(params.sessionId);
      return makeResponse(request.id, { artifacts });
    },

    'canvas.get': async (request: GatewayRequest): Promise<OutboundMessage> => {
      const params = request.params as { sessionId?: string; artifactId?: string } | undefined;
      if (!params?.sessionId) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'sessionId is required');
      }
      if (!params?.artifactId) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'artifactId is required');
      }

      const artifact = deps.store.get(params.sessionId, params.artifactId);
      if (!artifact) {
        return makeError(request.id, ErrorCode.SessionNotFound, 'Canvas artifact not found');
      }
      return makeResponse(request.id, { artifact });
    },

    'canvas.put': async (request: GatewayRequest): Promise<OutboundMessage> => {
      const params = request.params as {
        sessionId?: string;
        artifactId?: string;
        type?: string;
        title?: string;
        content?: unknown;
        metadata?: Record<string, unknown>;
      } | undefined;
      if (!params?.sessionId) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'sessionId is required');
      }
      if (!params?.type || typeof params.type !== 'string' || !params.type.trim()) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'type is required');
      }
      if (params.content === undefined) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'content is required');
      }

      const artifact = deps.store.put(params.sessionId, {
        id: params.artifactId,
        type: params.type,
        title: params.title,
        content: params.content,
        metadata: params.metadata,
      });
      return makeResponse(request.id, { artifact, upserted: true });
    },

    'canvas.delete': async (request: GatewayRequest): Promise<OutboundMessage> => {
      const params = request.params as { sessionId?: string; artifactId?: string } | undefined;
      if (!params?.sessionId) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'sessionId is required');
      }
      if (!params?.artifactId) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'artifactId is required');
      }

      const deleted = deps.store.delete(params.sessionId, params.artifactId);
      return makeResponse(request.id, { deleted });
    },

    'canvas.clear': async (request: GatewayRequest): Promise<OutboundMessage> => {
      const params = request.params as { sessionId?: string } | undefined;
      if (!params?.sessionId) {
        return makeError(request.id, ErrorCode.InvalidRequest, 'sessionId is required');
      }

      const cleared = deps.store.clear(params.sessionId);
      return makeResponse(request.id, { cleared });
    },
  };
}
