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

export interface RunScenarioRequest {
  scenarioId: string;
  iso2: string;
}

export interface RunScenarioResponse {
  jobId: string;
  status: string;
  statusUrl: string;
}

export interface GetScenarioStatusRequest {
  jobId: string;
}

export interface GetScenarioStatusResponse {
  status: string;
  result?: ScenarioResult;
  error: string;
}

export interface ScenarioResult {
  affectedChokepointIds: string[];
  topImpactCountries: ScenarioImpactCountry[];
  template?: ScenarioResultTemplate;
}

export interface ScenarioImpactCountry {
  iso2: string;
  totalImpact: number;
  impactPct: number;
}

export interface ScenarioResultTemplate {
  name: string;
  disruptionPct: number;
  durationDays: number;
  costShockMultiplier: number;
}

export interface ListScenarioTemplatesRequest {
}

export interface ListScenarioTemplatesResponse {
  templates: ScenarioTemplate[];
}

export interface ScenarioTemplate {
  id: string;
  name: string;
  affectedChokepointIds: string[];
  disruptionPct: number;
  durationDays: number;
  affectedHs2: string[];
  costShockMultiplier: 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 ScenarioServiceClientOptions {
  fetch?: typeof fetch;
  defaultHeaders?: Record<string, string>;
}

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

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

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

  async runScenario(req: RunScenarioRequest, options?: ScenarioServiceCallOptions): Promise<RunScenarioResponse> {
    let path = "/api/scenario/v1/run-scenario";
    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 RunScenarioResponse;
  }

  async getScenarioStatus(req: GetScenarioStatusRequest, options?: ScenarioServiceCallOptions): Promise<GetScenarioStatusResponse> {
    let path = "/api/scenario/v1/get-scenario-status";
    const params = new URLSearchParams();
    if (req.jobId != null && req.jobId !== "") params.set("jobId", String(req.jobId));
    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 GetScenarioStatusResponse;
  }

  async listScenarioTemplates(_req: ListScenarioTemplatesRequest, options?: ScenarioServiceCallOptions): Promise<ListScenarioTemplatesResponse> {
    let path = "/api/scenario/v1/list-scenario-templates";
    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 ListScenarioTemplatesResponse;
  }

  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);
  }
}

