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

export interface ListSanctionsPressureRequest {
  maxItems: number;
}

export interface ListSanctionsPressureResponse {
  entries: SanctionsEntry[];
  countries: CountrySanctionsPressure[];
  programs: ProgramSanctionsPressure[];
  fetchedAt: string;
  datasetDate: string;
  totalCount: number;
  sdnCount: number;
  consolidatedCount: number;
  newEntryCount: number;
  vesselCount: number;
  aircraftCount: number;
}

export interface SanctionsEntry {
  id: string;
  name: string;
  entityType: SanctionsEntityType;
  countryCodes: string[];
  countryNames: string[];
  programs: string[];
  sourceLists: string[];
  effectiveAt: string;
  isNew: boolean;
  note: string;
}

export interface CountrySanctionsPressure {
  countryCode: string;
  countryName: string;
  entryCount: number;
  newEntryCount: number;
  vesselCount: number;
  aircraftCount: number;
}

export interface ProgramSanctionsPressure {
  program: string;
  entryCount: number;
  newEntryCount: number;
}

export interface LookupSanctionEntityRequest {
  q: string;
  maxResults: number;
}

export interface LookupSanctionEntityResponse {
  results: SanctionEntityMatch[];
  total: number;
  source: string;
}

export interface SanctionEntityMatch {
  id: string;
  name: string;
  entityType: string;
  countryCodes: string[];
  programs: string[];
}

export type SanctionsEntityType = "SANCTIONS_ENTITY_TYPE_UNSPECIFIED" | "SANCTIONS_ENTITY_TYPE_ENTITY" | "SANCTIONS_ENTITY_TYPE_INDIVIDUAL" | "SANCTIONS_ENTITY_TYPE_VESSEL" | "SANCTIONS_ENTITY_TYPE_AIRCRAFT";

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 SanctionsServiceHandler {
  listSanctionsPressure(ctx: ServerContext, req: ListSanctionsPressureRequest): Promise<ListSanctionsPressureResponse>;
  lookupSanctionEntity(ctx: ServerContext, req: LookupSanctionEntityRequest): Promise<LookupSanctionEntityResponse>;
}

export function createSanctionsServiceRoutes(
  handler: SanctionsServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "GET",
      path: "/api/sanctions/v1/list-sanctions-pressure",
      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: ListSanctionsPressureRequest = {
            maxItems: Number(params.get("max_items") ?? "0"),
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("listSanctionsPressure", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.listSanctionsPressure(ctx, body);
          return new Response(JSON.stringify(result as ListSanctionsPressureResponse), {
            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/sanctions/v1/lookup-sanction-entity",
      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: LookupSanctionEntityRequest = {
            q: params.get("q") ?? "",
            maxResults: Number(params.get("max_results") ?? "0"),
          };
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("lookupSanctionEntity", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

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

