import api from "src/api";

export const getPartnerAgreements = async ({ type } = {}) => {
  const params = {};
  if (type) params.type = type;
  const { data } = await api.get("/associado-partner-agreement", { params });
  return data.payload;
};

export const getPartnerAgreementsPaginated = async ({ page = 1, perPage = 10, filter, expiresInDays, createdMonth, type } = {}) => {
  const params = { page, per_page: perPage };
  if (filter)        params.search          = filter;
  if (expiresInDays) params.expires_in_days = expiresInDays;
  if (createdMonth)  params.created_month   = createdMonth;
  if (type)          params.type            = type;
  const { data } = await api.get("/partner-agreement/paginated", { params });
  return { data: { result: data.payload } };
};

export const getExpiringPartnerAgreementsPaginated = async ({ page = 1, perPage = 10, days = 30 } = {}) => {
  const { data } = await api.get("/partner-agreement/expiring", { params: { page, per_page: perPage, days } });
  return { data: { result: data.payload } };
};

export const getPartnerAgreement = async (id) => {
  const { data } = await api.get(`/partner-agreement/${id}`);
  return data.payload;
};

export const getPartnerAgreementDados = async (id) => {
  const { data } = await api.get(`/partner-agreement/${id}/dados`);
  return data.payload;
};

export const getPartnerAgreementContato = async (id) => {
  const { data } = await api.get(`/partner-agreement/${id}/contato`);
  return data.payload;
};

export const getPartnerAgreementEndereco = async (id) => {
  const { data } = await api.get(`/partner-agreement/${id}/endereco`);
  return data.payload;
};

export const getPartnerAgreementContrato = async (id) => {
  const { data } = await api.get(`/partner-agreement/${id}/contrato`);
  return data.payload;
};

export const createPartnerAgreement = async (payload) => {
  const { data } = await api.post("/partner-agreement", payload);
  return data.payload;
};

export const updatePartnerAgreement = async (id, payload) => {
  const { data } = await api.put(`/partner-agreement/${id}`, payload);
  return data.payload;
};

export const deletePartnerAgreement = async (id) => {
  await api.delete(`/partner-agreement/${id}`);
};

export const uploadPartnerLogo = async (id, file) => {
  const form = new FormData();
  form.append("logo", file);
  const { data } = await api.post(`/partner-agreement/${id}/logo`, form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};

export const uploadPartnerMedia = async (id, file) => {
  const form = new FormData();
  form.append("file", file);
  const { data } = await api.post(`/partner-agreement/${id}/media`, form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};

export const deletePartnerMedia = async (id, mediaId) => {
  await api.delete(`/partner-agreement/${id}/media/${mediaId}`);
};

// ─── Rotas do Associado ───────────────────────────────────────────────────────

export const getConvenios = async () => {
  const { data } = await api.get("/associado/partner-agreement");
  return data.payload;
};

export const getConvenio = async (id) => {
  const { data } = await api.get(`/associado/partner-agreement/${id}`);
  return data.payload;
};

export const getConvenioDados = async (id) => {
  const { data } = await api.get(`/associado/partner-agreement/${id}/dados`);
  return data.payload;
};

// ─── Rotas do Parceiro (my) ───────────────────────────────────────────────────

export const getMyPartnerAgreement = async () => {
  const { data } = await api.get("/partner-agreement/my");
  return data.payload;
};

export const updateMyPartnerAgreement = async (payload) => {
  const { data } = await api.put("/partner-agreement/my", payload);
  return data.payload;
};

export const uploadMyPartnerLogo = async (file) => {
  const form = new FormData();
  form.append("logo", file);
  const { data } = await api.post("/partner-agreement/my/logo", form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};

export const uploadMyPartnerMedia = async (file) => {
  const form = new FormData();
  form.append("file", file);
  const { data } = await api.post("/partner-agreement/my/media", form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};

export const deleteMyPartnerMedia = async (mediaId) => {
  await api.delete(`/partner-agreement/my/media/${mediaId}`);
};

export const importParceiros = async (file) => {
  const form = new FormData();
  form.append("file", file);
  const { data } = await api.post("/partner-agreement/import/parceiros", form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};

export const importConveniosMedicos = async (file) => {
  const form = new FormData();
  form.append("file", file);
  const { data } = await api.post("/partner-agreement/import/convenios-medicos", form, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data.payload;
};
