/*
 * Mirai. Copyright (C) 2024 Vyn
 * This file is licensed under version 3 of the GNU General Public License (GPL-3.0-only)
 * The license can be found in the LICENSE file or at https://www.gnu.org/licenses/gpl-3.0.txt
 */

#pragma once

#include "DataProvider.h"
#include "DateTime.h"
#include <algorithm>
#include <fstream>
#include <iterator>
#include <optional>
#include <ranges>
#include <string>
#include <vector>

namespace mirai
{

struct MarkdownData {
	std::string name;
	std::vector<TaskData> tasks;
	std::vector<DayData> days;
	std::vector<EventData> events;
};

class MarkdownDataProvider : public DataProvider
{
  public:
	MarkdownDataProvider(const std::string &filePath) : filePath_(filePath), DataProvider()
	{
	}

	std::string toMarkdown();
	MarkdownData parseMarkdown(const std::string &content);

	void save() override;
	void load() override;

	std::string name() const override;

	// Tasks
	TaskData insertTask(const TaskData &task) override;
	void removeTaskById(int taskId) override;
	std::optional<TaskData> getTaskById(int taskId) override;
	std::vector<TaskData> getTasksByEventId(int eventId) override;
	std::vector<TaskData> getTasksByDayId(int dayId) override;
	std::vector<TaskData> getTasksByDate(Date date) override;
	std::vector<TaskData> getTasksWithoutDate() override;
	std::vector<TaskData> getTasks() override;
	void updateTask(int taskId, UpdatableTaskData updateData) override;

	// Events
	EventData insertEvent(const EventData &task) override;
	void removeEventById(int eventId) override;
	std::optional<EventData> getEventById(int eventId) override;
	std::vector<EventData> getEvents() override;
	std::vector<EventData> getEventsByDate(Date date) override;
	void updateEvent(int eventId, UpdatableEventData updateData) override;

	// Days
	DayData insertDay(const DayData &day) override;
	void removeDayById(int dayId) override;
	std::vector<DayData> getDays() override;
	std::optional<DayData> getDayByDate(const Date &date) override;
	void updateDay(int dayId, UpdatableDayData updateData) override;

  private:
	std::string name_;
	std::string filePath_;
	MarkdownData data;
};

} // namespace mirai
