#pragma once

#include "queue.hpp"
#include <chrono>
#include <fstream>
#include <print>
#include <string>
#include <thread>
#include <vector>

namespace workers {
void producer(th_safe::queue<std::string> &q,
              const std::vector<std::string> &files, int id) {
  for (const auto &file : files) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));

    q.push(file);
    std::print("Produced ({}): {}\n", id, file);
  }
}

void consumer(th_safe::queue<std::string> &q, int id) {
  while (true) {
    auto item = q.pop();
    if (!item.has_value())
      break;

    std::ifstream file(item.value());
    if (!file) {
      std::print("Failed to open: {}\n", item.value());
      continue;
    }

    std::print("Consumed ({}): {}\n", id, item.value());
    for (std::string line; std::getline(file, line);) {
      std::print("\t{}\n", line);
    }
  }
}
} // namespace workers
