#ifndef DAILY_AND_SIZE_SINK_H
#define DAILY_AND_SIZE_SINK_H

#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/file_helper.h>
#include <mutex>
#include <string>
#include <chrono>
#include <filesystem>


namespace zsy
{
    class DailyAndSizeSink final : public spdlog::sinks::base_sink<std::mutex>
    {
        std::string basePath;
        std::string baseFilename;
        std::string baseExt;
        uintmax_t maxSize;
        int32_t maxDays;
        std::string currentDate;
        uint32_t currentIndex = 0;
        uintmax_t currentSize = 0;
        std::string currentFilename;
        spdlog::details::file_helper fileHelper;

        void rotateFile();

        void cleanOldFiles();

        uint32_t lastIndex();

        uintmax_t lastSize();

    public:
        DailyAndSizeSink(const std::string &filename, size_t maxSize, int32_t maxDays);

    protected:
        void sink_it_(const spdlog::details::log_msg &msg) override;

        void flush_() override;
    };
}

#endif //DAILY_AND_SIZE_SINK_H