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

const FILE_NAME = "object.ts";

export function generateObjectFetcher(outputDir: string): void {
  const content = `${generateFileHeader(FILE_NAME)}
import { AttioClient } from "./attioClient";
import { BaseFetcher } from "./base";
import { AttioObject } from "../types";

export interface AttioObjectInput {
  data: {
    api_slug: string;
    singular_noun: string;
    plural_noun: string;
  };
}

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

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

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

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

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

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

  writeGeneratedFile(outputDir, FILE_NAME, content);
}
