import api from "../axios";

export interface CredentialField {
  id: string;
  name: string;
  secure: boolean;
  required: boolean;
  valueType: string;
  value: string;
}

export interface Credential {
  id: string;
  name: string;
  notes?: string;
  typeId: string;
  companyId: string;
  subCredentialOfId?: string;
  fields: CredentialField[];
  type?: {
    id: string;
    name: string;
    fields: unknown[];
    permissionScope: string;
  };
  company?: {
    id: string;
    name: string;
  };
  createdAt: string;
  updatedAt: string;
}

export const credential = {
  async fetchByCompany(accessToken: string, companyId: string) {
    const response = await api.get(
      `/v1/credential/credentials/company/${companyId}`,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      },
    );
    return response.data;
  },

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

    return response.data;
  },

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

  async update(
    accessToken: string,
    id: string,
    data: { name?: string; notes?: string },
  ) {
    const response = await api.patch(`/v1/credential/credentials/${id}`, data, {
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    return response.data;
  },

  async updateFields(
    accessToken: string,
    id: string,
    fields: CredentialField[],
  ) {
    const response = await api.put(
      `/v1/credential/credentials/${id}/fields`,
      { fields },
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      },
    );
    return response.data;
  },

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

  async fetchSecureValue(
    accessToken: string,
    credentialId: string,
    fieldId: string,
  ) {
    const response = await api.get(
      `/v1/credential/credentials/${credentialId}/secure-values/${fieldId}`,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      },
    );

    return response.data;
  },

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

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

  async addSubCredential(
    accessToken: string,
    credentialId: string,
    data: {
      fieldId: string;
      name: string;
      fields: Array<{ fieldId: string; value: string }>;
    },
  ) {
    const response = await api.post(
      `/v1/credential/credentials/${credentialId}/sub-credentials`,
      data,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      },
    );
    return response.data;
  },

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