// @ts-nocheck
// Code generated by protoc-gen-ts-server. DO NOT EDIT.
// source: worldmonitor/prediction/v1/service.proto

export interface ListPredictionMarketsRequest {
  pageSize: number;
  cursor: string;
  category: string;
  query: string;
}

export interface ListPredictionMarketsResponse {
  markets: PredictionMarket[];
  pagination?: PaginationResponse;
}

export interface PredictionMarket {
  id: string;
  title: string;
  yesPrice: number;
  volume: number;
  url: string;
  closesAt: number;
  category: string;
  source: MarketSource;
}

export interface PaginationResponse {
  nextCursor: string;
  totalCount: number;
}

export type MarketSource = "MARKET_SOURCE_UNSPECIFIED" | "MARKET_SOURCE_POLYMARKET" | "MARKET_SOURCE_KALSHI";

export interface FieldViolation {
  field: string;
  description: string;
}

export class ValidationError extends Error {
  violations: FieldViolation[];

  constructor(violations: FieldViolation[]) {
    super("Validation failed");
    this.name = "ValidationError";
    this.violations = violations;
  }
}

export class ApiError extends Error {
  statusCode: number;
  body: string;

  constructor(statusCode: number, message: string, body: string) {
    super(message);
    this.name = "ApiError";
    this.statusCode = statusCode;
    this.body = body;
  }
}

export interface ServerContext {
  request: Request;
  pathParams: Record<string, string>;
  headers: Record<string, string>;
}

export interface ServerOptions {
  onError?: (error: unknown, req: Request) => Response | Promise<Response>;
  validateRequest?: (methodName: string, body: unknown) => FieldViolation[] | undefined;
}

export interface RouteDescriptor {
  method: string;
  path: string;
  handler: (req: Request) => Promise<Response>;
}

export interface PredictionServiceHandler {
  listPredictionMarkets(ctx: ServerContext, req: ListPredictionMarketsRequest): Promise<ListPredictionMarketsResponse>;
}

export function createPredictionServiceRoutes(
  handler: PredictionServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "GET",
      path: "/api/prediction/v1/list-prediction-markets",
      handler: async (req: Request): Promise<Response> => {
        try {
          const pathParams: Record<string, string> = {};
          const url = new URL(req.url, "http://localhost");
          const params = url.searchParams;
          const body: ListPredictionMarketsRequest = {
            pageSize: Number(params.get("page_size") ?? "0"),
            cursor: params.get("cursor") ?? "",
            category: params.get("category") ?? "",
            query: params.get("query") ?? "",
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listPredictionMarkets", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

          const ctx: ServerContext = {
            request: req,
            pathParams,
            headers: Object.fromEntries(req.headers.entries()),
          };

          const result = await handler.listPredictionMarkets(ctx, body);
          return new Response(JSON.stringify(result as ListPredictionMarketsResponse), {
            status: 200,
            headers: { "Content-Type": "application/json" },
          });
        } catch (err: unknown) {
          if (err instanceof ValidationError) {
            return new Response(JSON.stringify({ violations: err.violations }), {
              status: 400,
              headers: { "Content-Type": "application/json" },
            });
          }
          if (options?.onError) {
            return options.onError(err, req);
          }
          const message = err instanceof Error ? err.message : String(err);
          return new Response(JSON.stringify({ message }), {
            status: 500,
            headers: { "Content-Type": "application/json" },
          });
        }
      },
    },
  ];
}

