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

export interface ListWebcamsRequest {
  zoom: number;
  boundW: number;
  boundS: number;
  boundE: number;
  boundN: number;
}

export interface ListWebcamsResponse {
  webcams: WebcamEntry[];
  clusters: WebcamCluster[];
  totalInView: number;
}

export interface WebcamEntry {
  webcamId: string;
  title: string;
  lat: number;
  lng: number;
  category: string;
  country: string;
}

export interface WebcamCluster {
  lat: number;
  lng: number;
  count: number;
  categories: string[];
}

export interface GetWebcamImageRequest {
  webcamId: string;
}

export interface GetWebcamImageResponse {
  thumbnailUrl: string;
  playerUrl: string;
  title: string;
  windyUrl: string;
  lastUpdated: string;
  error: string;
}

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 WebcamServiceHandler {
  listWebcams(ctx: ServerContext, req: ListWebcamsRequest): Promise<ListWebcamsResponse>;
  getWebcamImage(ctx: ServerContext, req: GetWebcamImageRequest): Promise<GetWebcamImageResponse>;
}

export function createWebcamServiceRoutes(
  handler: WebcamServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "GET",
      path: "/api/webcam/v1/list-webcams",
      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: ListWebcamsRequest = {
            zoom: Number(params.get("zoom") ?? "0"),
            boundW: Number(params.get("bound_w") ?? "0"),
            boundS: Number(params.get("bound_s") ?? "0"),
            boundE: Number(params.get("bound_e") ?? "0"),
            boundN: Number(params.get("bound_n") ?? "0"),
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listWebcams", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listWebcams(ctx, body);
          return new Response(JSON.stringify(result as ListWebcamsResponse), {
            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/webcam/v1/get-webcam-image",
      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: GetWebcamImageRequest = {
            webcamId: params.get("webcam_id") ?? "",
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("getWebcamImage", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

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

