import axios from "../axios.js";
import fs from "fs";
export const getBids = async () => {
  try {
    const { data } = await axios({
      method: "GET",
      url: "bids",
    });

    if (!data || !data?.data) {
      console.log("❌ DATA IS NOT FOUND ON SERVER");
      return [];
    }

    return data.data;
  } catch (error) {
    console.log("❌ ERROR IN SERVER (GET BIDS): ", error);
    return [];
  }
};

export const updateBid = async (id, values) => {
  try {
    const { data } = await axios({
      method: "PUT",
      url: "bids/" + id,
      data: values,
    });

    if (!data || !data?.data) {
      console.log("❌ UPDATE FAILURE (UPDATE BID)");
      return null;
    }

    return data.data;
  } catch (error) {
    console.log("❌ ERROR IN SERVER: (UPDATE BID) ", error.response);
    return null;
  }
};

export const outBid = async (id) => {
  try {
    const { data } = await axios({
      method: "POST",
      url: "bids/out-bid/" + id,
    });

    if (!data || !data?.data) {
      console.log("❌ OUT BID UPDATE FAILURE");
      return false;
    }

    return data.data;
  } catch (error) {
    console.log("❌ ERROR IN SERVER (OUT BID UPDATE): ", error);
    return false;
  }
};

export const pushPrice = async (values) => {
  try {
    const { data } = await axios({
      method: "POST",
      url: "bid-histories",
      data: values,
    });

    if (!data || !data?.data) {
      console.log("❌ PUSH PRICE FAILURE");
      return { status: false, data: [] };
    }

    return { status: true, data: data.data };
  } catch (error) {
    console.log("❌ ERROR IN SERVER (PUSH PRICE): ", error?.response);
    return { status: false, data: [] };
  }
};

export const updateStatusByPrice = async (id, current_price) => {
  try {
    const { data } = await axios({
      method: "POST",
      url: "bids/update-status/" + id,
      data: {
        current_price: Number(current_price) | 0,
      },
    });

    if (!data || !data?.data) {
      console.log("❌ UPDATE STATUS BY PRICE FAILURE");
      return { status: false, data: [] };
    }

    return { status: true, data: data.data };
  } catch (error) {
    console.log("❌ ERROR IN SERVER:(UPDATE STATUS BY PRICE) ", {
      // response: error.response,
      message: error.message,
    });
    return { status: false, data: [] };
  }
};

export const updateStatusWork = async (item, filePath) => {
  try {
    const response = await axios({
      method: "POST",
      headers: {
        "Content-Type": "multipart/form-data",
      },
      url: `bids/update-status-work/${item.type}/${item.id}`,
      data: {
        image: fs.createReadStream(filePath),
      },
    });
    fs.unlinkSync(filePath);

    return response.data?.data;
  } catch (error) {
    console.error("❌ Upload failed:", error.response?.data || error.message);
    return false;
  }
};

export const uploadRecord = async (item, filePath) => {
  try {
    const response = await axios({
      method: "POST",
      headers: {
        "Content-Type": "multipart/form-data",
      },
      url: `bids/upload-record/${item.id}`,
      data: {
        video: fs.createReadStream(filePath),
      },
    });
    fs.unlinkSync(filePath);

    return response.data?.data;
  } catch (error) {
    console.error("❌ Upload failed:", error.response?.data || error.message);
    return false;
  }
};

export const updateLoginStatus = async (data) => {
  try {
    const response = await axios({
      method: "POST",
      url: `bids/update-login-status`,
      data,
    });

    return response.data?.data;
  } catch (error) {
    console.error(
      "❌ UPDATE FAILURE (LOGIN STATUS):",
      error.response?.data || error.message
    );
    return false;
  }
};
