/*
 * Author:          Patrick-Christopher Mattulat
 * Co-Author:       Claude Sonnet 4.6 (LLM)
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2020-08-15
 * Changed:         2026-06-23
 *
 * */

#ifndef LS_STD_FILE_HPP
#define LS_STD_FILE_HPP

#include <ctime>
#if defined(unix) || defined(__APPLE__)
  #include <dirent.h>
#endif
#include <list>
#include <ls-std/core/Class.hpp>
#include <ls-std/os/dynamic-goal.hpp>
#include <string>
#include <vector>
#ifdef _WIN32
  #define NOMINMAX
  #include <windows.h>
#endif

/*
 * @doc: class(name: 'File', package: 'io')
 * @doc: io.File.description('This class represents a file in the file system and provides functionalities for location, operations and fetching property information.')
 * */

namespace ls::standard::io
{
  class LS_STD_DYNAMIC_GOAL File : public ls::standard::core::Class
  {
    public:

      explicit File(::std::string _absoluteFilePath);
      ~File() noexcept override;

      // comparison operators

      bool operator==(const ls::standard::io::File &_file) const;
      bool operator!=(const ls::standard::io::File &_file) const;

      // additional functionality

      [[nodiscard]] bool canExecute() const;
      [[nodiscard]] bool canRead() const;
      [[nodiscard]] bool canWrite() const;
      void createNewFile() const;
      [[nodiscard]] bool exists() const;
      [[nodiscard]] ::std::string getAbsoluteFilePath() const;
      [[nodiscard]] ::std::string getName() const;
      [[nodiscard]] ::std::string getParent() const;
      [[nodiscard]] size_t getSize() const;
      [[nodiscard]] static ::std::string getWorkingDirectory();
      [[nodiscard]] bool isDirectory() const;
      [[nodiscard]] bool isFile() const;
      [[nodiscard]] time_t lastModified() const;
      [[nodiscard]] ::std::list<::std::string> list() const;
      [[nodiscard]] ::std::list<::std::string> listFiles() const;
      void makeDirectory() const;
      void makeDirectories() const;
      void remove() const;
      bool renameTo(const ::std::string &_newName); // nodiscard is optional here
      void reset(const ::std::string &_newPath);

    private:

      ::std::string absoluteFilePath{};

#if defined(unix) || defined(__APPLE__)
      static void _addToFileListUnix(const ::std::string &_path, bool _withDirectories, const dirent *directoryEntity, ::std::list<::std::string> &_list);
#endif
#ifdef _WIN32
      static void _addToFileListWindows(const ::std::string &_path, bool _withDirectories, WIN32_FIND_DATA _data, ::std::list<::std::string> &_list);
#endif
      [[nodiscard]] static bool _equals(const ls::standard::io::File &_file, const ls::standard::io::File &_foreignFile);
      [[nodiscard]] static bool _exists(const ::std::string &_path);
      [[nodiscard]] static ::std::string _getParent(const ::std::string &_path);
#if defined(unix) || defined(__APPLE__)
      [[nodiscard]] static ::std::string _getWorkingDirectoryUnix();
#endif
#ifdef _WIN32
      [[nodiscard]] static ::std::string _getWorkingDirectoryWindows();
#endif
      [[nodiscard]] static bool _isDirectory(const ::std::string &_path);
      [[nodiscard]] static bool _isExecutable(const ::std::string &_path);
      [[nodiscard]] static bool _isFile(const ::std::string &_path);
#if defined(unix) || defined(__APPLE__)
      [[nodiscard]] static bool _isReadableUnix(const ::std::string &_path);
#endif
#ifdef _WIN32
      [[nodiscard]] static bool _isReadableWindows(const ::std::string &_path);
#endif
      [[nodiscard]] static bool _isWritable(const ::std::string &_path);
      [[nodiscard]] static time_t _lastModified(const ::std::string &_path);
      [[nodiscard]] static ::std::list<::std::string> _list(const ::std::string &_path);
      [[nodiscard]] static ::std::list<::std::string> _listFiles(const ::std::string &_path);
#if defined(unix) || defined(__APPLE__)
      [[nodiscard]] static ::std::list<::std::string> _listUnix(const ::std::string &_path, bool withDirectories);
#endif
#ifdef _WIN32
      [[nodiscard]] static ::std::list<::std::string> _listWindows(const ::std::string &_path, bool withDirectories);
#endif
      [[nodiscard]] static bool _makeDirectory(const ::std::string &_path);
      [[nodiscard]] static ::std::string _normalizePath(::std::string _path);
      [[nodiscard]] static ::std::string _reduceSeparators(const ::std::string &_path);
      static void _remove(const ::std::string &_path);
#if defined(unix) || defined(__APPLE__)
      static void _removeUnix(const ::std::string &_path);
#endif
#ifdef _WIN32
      static void _removeWindows(const ::std::string &_path);
#endif
      [[nodiscard]] static bool _renameTo(const ::std::string &_oldName, const ::std::string &_newName);
      [[nodiscard]] static ::std::string _replaceWrongSeparator(::std::string _path);
      [[nodiscard]] static ::std::vector<::std::string> _splitIntoSubDirectoryNames(const ::std::string &_path);
  };
}

#endif
