import axios, { AxiosInstance } from 'axios';

export type PartnerName = '1inch' | '0x' | 'LiFi';

export interface ResolvePartnerPayloadsRequest {
  partner: PartnerName;
  amount: string;
  fromChainId?: number;
  toChainId?: number;
  routeType?: string;
  tokenIn?: string;
  tokenOut?: string;
  takerAddress?: string;
  fromAddress?: string;
  toAddress?: string;
  recipient?: string;
  slippagePercent?: string;
  slippageBps?: string;
  includeUnsupported?: boolean;
}

export interface PartnerPayloadTemplate {
  partner: PartnerName;
  routeId: string;
  supported: boolean;
  reason?: string;
  endpoint: string;
  method: 'GET';
  headers: Record<string, string>;
  query: Record<string, string>;
  route: {
    routeId: string;
    status: 'live';
    fromChainId: number;
    toChainId: number;
    routeType: 'swap' | 'bridge';
  };
  docs: string[];
}

export interface ResolvePartnerPayloadsResponse {
  generatedAt: string;
  format: 'partner-payload-templates-v1';
  partner: PartnerName;
  amount: string;
  count: number;
  supportedCount: number;
  payloads: PartnerPayloadTemplate[];
}

export class PartnerPayloadClient {
  private readonly http: AxiosInstance;

  constructor(baseUrl = 'http://localhost:3000') {
    this.http = axios.create({
      baseURL: baseUrl.replace(/\/+$/, ''),
      timeout: 15_000,
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
      },
    });
  }

  async resolvePartnerPayloads(
    request: ResolvePartnerPayloadsRequest
  ): Promise<ResolvePartnerPayloadsResponse> {
    const response = await this.http.post<ResolvePartnerPayloadsResponse>(
      '/api/v1/routes/partner-payloads/resolve',
      request
    );
    return response.data;
  }

  async getPartnerPayloads(
    request: ResolvePartnerPayloadsRequest
  ): Promise<ResolvePartnerPayloadsResponse> {
    const response = await this.http.get<ResolvePartnerPayloadsResponse>(
      '/api/v1/routes/partner-payloads',
      {
        params: request,
      }
    );
    return response.data;
  }
}

