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

export interface SubmitContactRequest {
  email: string;
  name: string;
  organization: string;
  phone: string;
  message: string;
  source: string;
  website: string;
  turnstileToken: string;
}

export interface SubmitContactResponse {
  status: string;
  emailSent: boolean;
}

export interface RegisterInterestRequest {
  email: string;
  source: string;
  appVersion: string;
  referredBy: string;
  website: string;
  turnstileToken: string;
}

export interface RegisterInterestResponse {
  status: string;
  referralCode: string;
  referralCount: number;
  position: number;
  emailSuppressed: boolean;
}

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 LeadsServiceHandler {
  submitContact(ctx: ServerContext, req: SubmitContactRequest): Promise<SubmitContactResponse>;
  registerInterest(ctx: ServerContext, req: RegisterInterestRequest): Promise<RegisterInterestResponse>;
}

export function createLeadsServiceRoutes(
  handler: LeadsServiceHandler,
  options?: ServerOptions,
): RouteDescriptor[] {
  return [
    {
      method: "POST",
      path: "/api/leads/v1/submit-contact",
      handler: async (req: Request): Promise<Response> => {
        try {
          const pathParams: Record<string, string> = {};
          const body = await req.json() as SubmitContactRequest;
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("submitContact", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

          const result = await handler.submitContact(ctx, body);
          return new Response(JSON.stringify(result as SubmitContactResponse), {
            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/leads/v1/register-interest",
      handler: async (req: Request): Promise<Response> => {
        try {
          const pathParams: Record<string, string> = {};
          const body = await req.json() as RegisterInterestRequest;
          if (options?.validateRequest) {
            const bodyViolations = options.validateRequest("registerInterest", body);
            if (bodyViolations) {
              throw new ValidationError(bodyViolations);
            }
          }

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

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

