import { writeGeneratedFile } from "../../helpers/fs";
import { generateFileHeader } from "../types/fileHeader";
import { generateWebhookEventFunctionForList } from "../webhookEventFactory";

const FILE_NAME = "list.ts";

export function generateListFetcher(outputDir: string): void {
  const content = `${generateFileHeader(FILE_NAME)}
import { AttioClient } from "./attioClient";
import { BaseFetcher } from "./base";
import { AttioWebhookEventPopulatedListener } from "./webhook";
import { AttioList, WebhookEventDataByType } from "../types";

export type AttioListInput = Pick<AttioList, "name" | "api_slug" | "parent_object" | "workspace_access" | "workspace_member_access">;

export class AttioListFetcher extends BaseFetcher {
  constructor(client: AttioClient) {
    super(client);
  }

  protected createBaseUrl(): string {
    return "/lists";
  }

  async getAll(): Promise<AttioList[]> {
    return this.extractData(
      this.client.doFetch(this.createBaseUrl())
    );
  }

  async create(data: AttioListInput): Promise<AttioList> {
    return this.extractData(
      this.client.doFetch(this.createBaseUrl(), {
        method: "POST",
        body: data,
      })
    );
  }

  async getById(id: string): Promise<AttioList> {
    return this.extractData(
      this.client.doFetch(\`\${this.createBaseUrl()}/\${id}\`)
    );
  }

  async update(id: string, data: AttioListInput): Promise<AttioList> {
    return this.extractData(
      this.client.doFetch(\`\${this.createBaseUrl()}/\${id}\`, {
        method: "PUT",
        body: data,
      })
    );
  }

  ${generateWebhookEventFunctionForList({
    eventNames: ["list.created", "list.updated", "list.deleted"],
    idKey: "list_id",
    populatedType: "AttioList",
  })}
}
`;

  writeGeneratedFile(outputDir, FILE_NAME, content);
}
