#include <algorithm>
#include <concepts>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <iterator>
#include <ranges>
#include <string_view>
#include <vector>

namespace fs = std::filesystem;
namespace rn = std::ranges;

std::vector<fs::path> searchFiles(std::string_view keyWord, const fs::path& dirName) {
    std::vector<fs::path> paths;
    rn::transform(fs::directory_iterator(dirName),
                  fs::directory_iterator{},
                  std::back_inserter(paths),
                  [](const auto& entry) { return entry.path(); });

    std::vector<fs::path> res;
    rn::copy_if(paths, std::back_inserter(res), [keyWord](const auto& path) {
        std::ifstream file(path);
        const auto end = std::istream_iterator<std::string>{};
        return std::find_if(std::istream_iterator<std::string>(file),
                            end,
                            [keyWord](const auto& str) { return str == keyWord; }) != end;
    });

    return res;
}

int main() {
    std::cout << "Files which contains word Ala:\n";
    const auto& res = searchFiles("Ala", fs::current_path().string() + "/files");
    rn::copy(res, std::ostream_iterator<fs::path>(std::cout, "\n"));
}