// Code generated by protoc-gen-ts-client. 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 ConflictServiceClientOptions {
  fetch?: typeof fetch;
  defaultHeaders?: Record<string, string>;
}

export interface ConflictServiceCallOptions {
  headers?: Record<string, string>;
  signal?: AbortSignal;
}

export class ConflictServiceClient {
  private baseURL: string;
  private fetchFn: typeof fetch;
  private defaultHeaders: Record<string, string>;

  constructor(baseURL: string, options?: ConflictServiceClientOptions) {
    this.baseURL = baseURL.replace(/\/+$/, "");
    this.fetchFn = options?.fetch ?? globalThis.fetch;
    this.defaultHeaders = { ...options?.defaultHeaders };
  }

  async listAcledEvents(req: ListAcledEventsRequest, options?: ConflictServiceCallOptions): Promise<ListAcledEventsResponse> {
    let path = "/api/conflict/v1/list-acled-events";
    const params = new URLSearchParams();
    if (req.start != null && req.start !== 0) params.set("start", String(req.start));
    if (req.end != null && req.end !== 0) params.set("end", String(req.end));
    if (req.pageSize != null && req.pageSize !== 0) params.set("page_size", String(req.pageSize));
    if (req.cursor != null && req.cursor !== "") params.set("cursor", String(req.cursor));
    if (req.country != null && req.country !== "") params.set("country", String(req.country));
    const url = this.baseURL + path + (params.toString() ? "?" + params.toString() : "");

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      ...this.defaultHeaders,
      ...options?.headers,
    };

    const resp = await this.fetchFn(url, {
      method: "GET",
      headers,
      signal: options?.signal,
    });

    if (!resp.ok) {
      return this.handleError(resp);
    }

    return await resp.json() as ListAcledEventsResponse;
  }

  async listUcdpEvents(req: ListUcdpEventsRequest, options?: ConflictServiceCallOptions): Promise<ListUcdpEventsResponse> {
    let path = "/api/conflict/v1/list-ucdp-events";
    const params = new URLSearchParams();
    if (req.start != null && req.start !== 0) params.set("start", String(req.start));
    if (req.end != null && req.end !== 0) params.set("end", String(req.end));
    if (req.pageSize != null && req.pageSize !== 0) params.set("page_size", String(req.pageSize));
    if (req.cursor != null && req.cursor !== "") params.set("cursor", String(req.cursor));
    if (req.country != null && req.country !== "") params.set("country", String(req.country));
    const url = this.baseURL + path + (params.toString() ? "?" + params.toString() : "");

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      ...this.defaultHeaders,
      ...options?.headers,
    };

    const resp = await this.fetchFn(url, {
      method: "GET",
      headers,
      signal: options?.signal,
    });

    if (!resp.ok) {
      return this.handleError(resp);
    }

    return await resp.json() as ListUcdpEventsResponse;
  }

  async getHumanitarianSummary(req: GetHumanitarianSummaryRequest, options?: ConflictServiceCallOptions): Promise<GetHumanitarianSummaryResponse> {
    let path = "/api/conflict/v1/get-humanitarian-summary";
    const params = new URLSearchParams();
    if (req.countryCode != null && req.countryCode !== "") params.set("country_code", String(req.countryCode));
    const url = this.baseURL + path + (params.toString() ? "?" + params.toString() : "");

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      ...this.defaultHeaders,
      ...options?.headers,
    };

    const resp = await this.fetchFn(url, {
      method: "GET",
      headers,
      signal: options?.signal,
    });

    if (!resp.ok) {
      return this.handleError(resp);
    }

    return await resp.json() as GetHumanitarianSummaryResponse;
  }

  async listIranEvents(_req: ListIranEventsRequest, options?: ConflictServiceCallOptions): Promise<ListIranEventsResponse> {
    let path = "/api/conflict/v1/list-iran-events";
    const url = this.baseURL + path;

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      ...this.defaultHeaders,
      ...options?.headers,
    };

    const resp = await this.fetchFn(url, {
      method: "GET",
      headers,
      signal: options?.signal,
    });

    if (!resp.ok) {
      return this.handleError(resp);
    }

    return await resp.json() as ListIranEventsResponse;
  }

  async getHumanitarianSummaryBatch(req: GetHumanitarianSummaryBatchRequest, options?: ConflictServiceCallOptions): Promise<GetHumanitarianSummaryBatchResponse> {
    let path = "/api/conflict/v1/get-humanitarian-summary-batch";
    const url = this.baseURL + path;

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      ...this.defaultHeaders,
      ...options?.headers,
    };

    const resp = await this.fetchFn(url, {
      method: "POST",
      headers,
      body: JSON.stringify(req),
      signal: options?.signal,
    });

    if (!resp.ok) {
      return this.handleError(resp);
    }

    return await resp.json() as GetHumanitarianSummaryBatchResponse;
  }

  private async handleError(resp: Response): Promise<never> {
    const body = await resp.text();
    if (resp.status === 400) {
      try {
        const parsed = JSON.parse(body);
        if (parsed.violations) {
          throw new ValidationError(parsed.violations);
        }
      } catch (e) {
        if (e instanceof ValidationError) throw e;
      }
    }
    throw new ApiError(resp.status, `Request failed with status ${resp.status}`, body);
  }
}

