import api from "../axios";

export interface CredentialTypeField {
  id: string;
  name: string;
  required: boolean;
  secure: boolean;
  valueType: string;
  subFields?: CredentialTypeField[];
}

export interface CredentialType {
  id: string;
  name: string;
  permissionScope: string;
  icon?: string;
  fields: CredentialTypeField[];
  credentialCount: number;
  createdAt: string;
  updatedAt: string;
}

export const credentialType = {
  async fetchMany(accessToken: string) {
    const response = await api.get("/v1/credential-type", {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },

  async fetch(accessToken: string, identifier: string) {
    const response = await api.get(`/v1/credential-type/${identifier}`, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },

  async create(
    accessToken: string,
    credentialType: Omit<
      CredentialType,
      "id" | "credentialCount" | "createdAt" | "updatedAt"
    >,
  ) {
    const response = await api.post("/v1/credential-type", credentialType, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },

  async update(
    accessToken: string,
    id: string,
    updates: Partial<
      Omit<CredentialType, "id" | "credentialCount" | "createdAt" | "updatedAt">
    >,
  ) {
    const response = await api.patch(`/v1/credential-type/${id}`, updates, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },

  async delete(accessToken: string, id: string) {
    const response = await api.delete(`/v1/credential-type/${id}`, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },

  async fetchCredentials(accessToken: string, id: string) {
    const response = await api.get(`/v1/credential-type/${id}/credentials`, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },
};
