import { search } from "booru";
import Post from "booru/dist/structures/Post"; // Ce n'est pas bien
import got from "got-cjs";
import { createWriteStream, createReadStream, unlink } from "fs";
import { promisify } from "node:util";
import stream from "node:stream";
import { exit } from "process";
import crashHandler from "./helpers/error.js";
import { config } from "./helpers/types.js";
import postImage from "./post.js";

/**
 * Get a remote image from a booru
 *
 * Currently this also downloads the image locally but idc nmp it gets deleted
 * @param conf Configuration object (see {@link config})
 * @calls postImage with the image it randomly selects from the booru given
 */
export default async function getRemoteImage(conf: config) {
  const searchResults = await search(conf.booru, conf.tags, {
    limit: 1,
    random: true,
  }).catch((e) => {
    crashHandler("Error searching for posts.", e);
    return [] as Post[];
  });
  if (searchResults.length === 0) {
    crashHandler("No posts found.", "");
    return;
  }
  const post = searchResults[0];
  // Set the post as sensitive if the rating is not safe
  const sensitivity: boolean = post.rating !== "s";
  // Make an HTTP request for the image
  const filename: string = post.fileUrl?.split("/").pop() as string; // Type checks for type checks
  const pipeline = promisify(stream.pipeline);

  await pipeline(
    got.stream(post.file_url as string),
    createWriteStream(filename)
  ).catch((err: Error) => {
    crashHandler("Error saving downloading image.", err);
    return;
  });

  const str = createReadStream(filename).on("error", (err: Error) => {
    crashHandler("Error reading downloaded image.", err);
  });

  // Make a status with the image
  await postImage(str, sensitivity, conf);

  // Delete the image that it downloaded
  unlink(filename, (err: unknown) => {
    if (err) {
      crashHandler("Error deleting downloaded image.", err);
      return;
    }
  });
  exit(0);
}
