#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;

    auto reg_file = [](fs::directory_entry e){return fs::is_regular_file(e);};

    for (const fs::directory_entry& entry : fs::directory_iterator(dirName) | std::views::filter(reg_file)) {
        std::ifstream file(entry.path());
        const auto res = std::find_if(std::istream_iterator<std::string>(file), std::istream_iterator<std::string>{},
                       [keyWord](const auto& str){ return str == keyWord; });
        if(res != std::istream_iterator<std::string>{})
            paths.push_back(entry.path());
    }

    return paths;
}

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"));
}
