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

export interface ListAcledEventsRequest {
  start: number;
  end: number;
  pageSize: number;
  cursor: string;
  country: string;
}

export interface ListAcledEventsResponse {
  events: AcledConflictEvent[];
  pagination?: PaginationResponse;
}

export interface AcledConflictEvent {
  id: string;
  eventType: string;
  country: string;
  location?: GeoCoordinates;
  occurredAt: number;
  fatalities: number;
  actors: string[];
  source: string;
  admin1: string;
}

export interface GeoCoordinates {
  latitude: number;
  longitude: number;
}

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

export interface ListUcdpEventsRequest {
  start: number;
  end: number;
  pageSize: number;
  cursor: string;
  country: string;
}

export interface ListUcdpEventsResponse {
  events: UcdpViolenceEvent[];
  pagination?: PaginationResponse;
}

export interface UcdpViolenceEvent {
  id: string;
  dateStart: number;
  dateEnd: number;
  location?: GeoCoordinates;
  country: string;
  sideA: string;
  sideB: string;
  deathsBest: number;
  deathsLow: number;
  deathsHigh: number;
  violenceType: UcdpViolenceType;
  sourceOriginal: string;
}

export interface GetHumanitarianSummaryRequest {
  countryCode: string;
}

export interface GetHumanitarianSummaryResponse {
  summary?: HumanitarianCountrySummary;
}

export interface HumanitarianCountrySummary {
  countryCode: string;
  countryName: string;
  conflictEventsTotal: number;
  conflictPoliticalViolenceEvents: number;
  conflictFatalities: number;
  referencePeriod: string;
  conflictDemonstrations: number;
  updatedAt: number;
}

export interface ListIranEventsRequest {
}

export interface ListIranEventsResponse {
  events: IranEvent[];
  scrapedAt: string;
}

export interface IranEvent {
  id: string;
  title: string;
  category: string;
  sourceUrl: string;
  latitude: number;
  longitude: number;
  locationName: string;
  timestamp: string;
  severity: string;
}

export interface GetHumanitarianSummaryBatchRequest {
  countryCodes: string[];
}

export interface GetHumanitarianSummaryBatchResponse {
  results: Record<string, HumanitarianCountrySummary>;
  fetched: number;
  requested: number;
}

export type UcdpViolenceType = "UCDP_VIOLENCE_TYPE_UNSPECIFIED" | "UCDP_VIOLENCE_TYPE_STATE_BASED" | "UCDP_VIOLENCE_TYPE_NON_STATE" | "UCDP_VIOLENCE_TYPE_ONE_SIDED";

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 ConflictServiceHandler {
  listAcledEvents(ctx: ServerContext, req: ListAcledEventsRequest): Promise<ListAcledEventsResponse>;
  listUcdpEvents(ctx: ServerContext, req: ListUcdpEventsRequest): Promise<ListUcdpEventsResponse>;
  getHumanitarianSummary(ctx: ServerContext, req: GetHumanitarianSummaryRequest): Promise<GetHumanitarianSummaryResponse>;
  listIranEvents(ctx: ServerContext, req: ListIranEventsRequest): Promise<ListIranEventsResponse>;
  getHumanitarianSummaryBatch(ctx: ServerContext, req: GetHumanitarianSummaryBatchRequest): Promise<GetHumanitarianSummaryBatchResponse>;
}

export function createConflictServiceRoutes(
  handler: ConflictServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "GET",
      path: "/api/conflict/v1/list-acled-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: ListAcledEventsRequest = {
            start: Number(params.get("start") ?? "0"),
            end: Number(params.get("end") ?? "0"),
            pageSize: Number(params.get("page_size") ?? "0"),
            cursor: params.get("cursor") ?? "",
            country: params.get("country") ?? "",
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listAcledEvents", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listAcledEvents(ctx, body);
          return new Response(JSON.stringify(result as ListAcledEventsResponse), {
            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/conflict/v1/list-ucdp-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: ListUcdpEventsRequest = {
            start: Number(params.get("start") ?? "0"),
            end: Number(params.get("end") ?? "0"),
            pageSize: Number(params.get("page_size") ?? "0"),
            cursor: params.get("cursor") ?? "",
            country: params.get("country") ?? "",
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listUcdpEvents", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listUcdpEvents(ctx, body);
          return new Response(JSON.stringify(result as ListUcdpEventsResponse), {
            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/conflict/v1/get-humanitarian-summary",
      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: GetHumanitarianSummaryRequest = {
            countryCode: params.get("country_code") ?? "",
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("getHumanitarianSummary", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.getHumanitarianSummary(ctx, body);
          return new Response(JSON.stringify(result as GetHumanitarianSummaryResponse), {
            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/conflict/v1/list-iran-events",
      handler: async (req: Request): Promise<Response> => {
        try {
          const pathParams: Record<string, string> = {};
          const body = {} as ListIranEventsRequest;

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

          const result = await handler.listIranEvents(ctx, body);
          return new Response(JSON.stringify(result as ListIranEventsResponse), {
            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: "POST",
      path: "/api/conflict/v1/get-humanitarian-summary-batch",
      handler: async (req: Request): Promise<Response> => {
        try {
          const pathParams: Record<string, string> = {};
          const body = await req.json() as GetHumanitarianSummaryBatchRequest;
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("getHumanitarianSummaryBatch", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

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

