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

export interface ListDiseaseOutbreaksRequest {
}

export interface ListDiseaseOutbreaksResponse {
  outbreaks: DiseaseOutbreakItem[];
  fetchedAt: number;
  alertLevelMethodologyVersion: string;
}

export interface DiseaseOutbreakItem {
  id: string;
  disease: string;
  location: string;
  countryCode: string;
  alertLevel: string;
  summary: string;
  sourceUrl: string;
  publishedAt: number;
  sourceName: string;
  lat: number;
  lng: number;
  cases: number;
}

export interface ListAirQualityAlertsRequest {
}

export interface ListAirQualityAlertsResponse {
  alerts: AirQualityAlert[];
  fetchedAt: number;
}

export interface AirQualityAlert {
  city: string;
  countryCode: string;
  lat: number;
  lng: number;
  pm25: number;
  aqi: number;
  riskLevel: string;
  pollutant: string;
  measuredAt: number;
  source: 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 HealthServiceHandler {
  listDiseaseOutbreaks(ctx: ServerContext, req: ListDiseaseOutbreaksRequest): Promise<ListDiseaseOutbreaksResponse>;
  listAirQualityAlerts(ctx: ServerContext, req: ListAirQualityAlertsRequest): Promise<ListAirQualityAlertsResponse>;
}

export function createHealthServiceRoutes(
  handler: HealthServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "GET",
      path: "/api/health/v1/list-disease-outbreaks",
      handler: async (req: Request): Promise<Response> => {
        try {
          const pathParams: Record<string, string> = {};
          const body = {} as ListDiseaseOutbreaksRequest;

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

          const result = await handler.listDiseaseOutbreaks(ctx, body);
          return new Response(JSON.stringify(result as ListDiseaseOutbreaksResponse), {
            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/health/v1/list-air-quality-alerts",
      handler: async (req: Request): Promise<Response> => {
        try {
          const pathParams: Record<string, string> = {};
          const body = {} as ListAirQualityAlertsRequest;

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

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

