import type { APIRequestContext, APIResponse } from '@playwright/test';

/**
 * Typisierter API-Wrapper für Playwright-Tests.
 * Ergänzt den raw request-Context um Auth-Header und JSON-Defaults.
 */
export class ApiClient {
  constructor(
    private readonly request: APIRequestContext,
    private readonly token: string,
  ) {}

  private get authHeaders() {
    return { Authorization: `Bearer ${this.token}` };
  }

  async get(path: string): Promise<APIResponse> {
    return this.request.get(path, { headers: this.authHeaders });
  }

  async post(path: string, body: unknown): Promise<APIResponse> {
    return this.request.post(path, {
      data: body,
      headers: this.authHeaders,
    });
  }

  async put(path: string, body: unknown): Promise<APIResponse> {
    return this.request.put(path, {
      data: body,
      headers: this.authHeaders,
    });
  }

  async patch(path: string, body: unknown): Promise<APIResponse> {
    return this.request.patch(path, {
      data: body,
      headers: this.authHeaders,
    });
  }

  async delete(path: string): Promise<APIResponse> {
    return this.request.delete(path, { headers: this.authHeaders });
  }
}
