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

export interface ListNaturalEventsRequest {
  days: number;
}

export interface ListNaturalEventsResponse {
  events: NaturalEvent[];
}

export interface NaturalEvent {
  id: string;
  title: string;
  description: string;
  category: string;
  categoryTitle: string;
  lat: number;
  lon: number;
  date: number;
  magnitude: number;
  magnitudeUnit: string;
  sourceUrl: string;
  sourceName: string;
  closed: boolean;
  stormId?: string;
  stormName?: string;
  basin?: string;
  stormCategory?: number;
  classification?: string;
  windKt?: number;
  pressureMb?: number;
  movementDir?: number;
  movementSpeedKt?: number;
  forecastTrack: ForecastPoint[];
  conePolygon: CoordRing[];
  pastTrack: PastTrackPoint[];
}

export interface ForecastPoint {
  lat: number;
  lon: number;
  hour: number;
  windKt: number;
  category: number;
}

export interface CoordRing {
  points: Coordinate[];
}

export interface Coordinate {
  lon: number;
  lat: number;
}

export interface PastTrackPoint {
  lat: number;
  lon: number;
  windKt: number;
  timestamp: number;
}

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 NaturalServiceHandler {
  listNaturalEvents(ctx: ServerContext, req: ListNaturalEventsRequest): Promise<ListNaturalEventsResponse>;
}

export function createNaturalServiceRoutes(
  handler: NaturalServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "GET",
      path: "/api/natural/v1/list-natural-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: ListNaturalEventsRequest = {
            days: Number(params.get("days") ?? "0"),
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listNaturalEvents", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

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

