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

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

export interface ListArxivPapersResponse {
  papers: ArxivPaper[];
  pagination?: PaginationResponse;
}

export interface ArxivPaper {
  id: string;
  title: string;
  summary: string;
  authors: string[];
  categories: string[];
  publishedAt: number;
  url: string;
}

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

export interface ListTrendingReposRequest {
  pageSize: number;
  cursor: string;
  language: string;
  period: string;
}

export interface ListTrendingReposResponse {
  repos: GithubRepo[];
  pagination?: PaginationResponse;
}

export interface GithubRepo {
  fullName: string;
  description: string;
  language: string;
  stars: number;
  starsToday: number;
  forks: number;
  url: string;
}

export interface ListHackernewsItemsRequest {
  pageSize: number;
  cursor: string;
  feedType: string;
}

export interface ListHackernewsItemsResponse {
  items: HackernewsItem[];
  pagination?: PaginationResponse;
}

export interface HackernewsItem {
  id: number;
  title: string;
  url: string;
  score: number;
  commentCount: number;
  by: string;
  submittedAt: number;
}

export interface ListTechEventsRequest {
  type: string;
  mappable: boolean;
  limit: number;
  days: number;
}

export interface ListTechEventsResponse {
  success: boolean;
  count: number;
  conferenceCount: number;
  mappableCount: number;
  lastUpdated: string;
  events: TechEvent[];
  error: string;
}

export interface TechEvent {
  id: string;
  title: string;
  type: string;
  location: string;
  coords?: TechEventCoords;
  startDate: string;
  endDate: string;
  url: string;
  source: string;
  description: string;
}

export interface TechEventCoords {
  lat: number;
  lng: number;
  country: string;
  original: string;
  virtual: boolean;
}

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 ResearchServiceHandler {
  listArxivPapers(ctx: ServerContext, req: ListArxivPapersRequest): Promise<ListArxivPapersResponse>;
  listTrendingRepos(ctx: ServerContext, req: ListTrendingReposRequest): Promise<ListTrendingReposResponse>;
  listHackernewsItems(ctx: ServerContext, req: ListHackernewsItemsRequest): Promise<ListHackernewsItemsResponse>;
  listTechEvents(ctx: ServerContext, req: ListTechEventsRequest): Promise<ListTechEventsResponse>;
}

export function createResearchServiceRoutes(
  handler: ResearchServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "GET",
      path: "/api/research/v1/list-arxiv-papers",
      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: ListArxivPapersRequest = {
            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("listArxivPapers", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listArxivPapers(ctx, body);
          return new Response(JSON.stringify(result as ListArxivPapersResponse), {
            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" },
          });
        }
      },
    },
    {
      method: "GET",
      path: "/api/research/v1/list-trending-repos",
      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: ListTrendingReposRequest = {
            pageSize: Number(params.get("page_size") ?? "0"),
            cursor: params.get("cursor") ?? "",
            language: params.get("language") ?? "",
            period: params.get("period") ?? "",
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listTrendingRepos", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listTrendingRepos(ctx, body);
          return new Response(JSON.stringify(result as ListTrendingReposResponse), {
            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" },
          });
        }
      },
    },
    {
      method: "GET",
      path: "/api/research/v1/list-hackernews-items",
      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: ListHackernewsItemsRequest = {
            pageSize: Number(params.get("page_size") ?? "0"),
            cursor: params.get("cursor") ?? "",
            feedType: params.get("feed_type") ?? "",
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listHackernewsItems", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listHackernewsItems(ctx, body);
          return new Response(JSON.stringify(result as ListHackernewsItemsResponse), {
            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" },
          });
        }
      },
    },
    {
      method: "GET",
      path: "/api/research/v1/list-tech-events",
      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: ListTechEventsRequest = {
            type: params.get("type") ?? "",
            mappable: params.get("mappable") === "true",
            limit: Number(params.get("limit") ?? "0"),
            days: Number(params.get("days") ?? "0"),
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listTechEvents", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listTechEvents(ctx, body);
          return new Response(JSON.stringify(result as ListTechEventsResponse), {
            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" },
          });
        }
      },
    },
  ];
}

