#ifndef EQEMU_CHARACTER_EXPEDITION_LOCKOUTS_REPOSITORY_H
#define EQEMU_CHARACTER_EXPEDITION_LOCKOUTS_REPOSITORY_H

#include "../database.h"
#include "../expedition_lockout_timer.h"
#include "../strings.h"
#include "base/base_character_expedition_lockouts_repository.h"
#include <unordered_map>

class CharacterExpeditionLockoutsRepository: public BaseCharacterExpeditionLockoutsRepository {
public:

    /**
     * This file was auto generated and can be modified and extended upon
     *
     * Base repository methods are automatically
     * generated in the "base" version of this repository. The base repository
     * is immutable and to be left untouched, while methods in this class
     * are used as extension methods for more specific persistence-layer
     * accessors or mutators.
     *
     * Base Methods (Subject to be expanded upon in time)
     *
     * Note: Not all tables are designed appropriately to fit functionality with all base methods
     *
     * InsertOne
     * UpdateOne
     * DeleteOne
     * FindOne
     * GetWhere(std::string where_filter)
     * DeleteWhere(std::string where_filter)
     * InsertMany
     * All
     *
     * Example custom methods in a repository
     *
     * CharacterExpeditionLockoutsRepository::GetByZoneAndVersion(int zone_id, int zone_version)
     * CharacterExpeditionLockoutsRepository::GetWhereNeverExpires()
     * CharacterExpeditionLockoutsRepository::GetWhereXAndY()
     * CharacterExpeditionLockoutsRepository::DeleteWhereXAndY()
     *
     * Most of the above could be covered by base methods, but if you as a developer
     * find yourself re-using logic for other parts of the code, its best to just make a
     * method that can be re-used easily elsewhere especially if it can use a base repository
     * method and encapsulate filters there
     */

	// Custom extended repository methods here

	struct CharacterExpeditionLockoutsTimeStamp {
		int         id;
		int         character_id;
		std::string expedition_name;
		std::string event_name;
		time_t      expire_time;
		int         duration;
		std::string from_expedition_uuid;
	};

	static ExpeditionLockoutTimer GetExpeditionLockoutTimerFromEntry(
		CharacterExpeditionLockoutsTimeStamp&& entry)
	{
		ExpeditionLockoutTimer lockout_timer{
			std::move(entry.from_expedition_uuid),
			std::move(entry.expedition_name),
			std::move(entry.event_name),
			static_cast<uint64_t>(entry.expire_time),
			static_cast<uint32_t>(entry.duration)
		};

		return lockout_timer;
	}

	static std::unordered_map<uint32_t, std::vector<ExpeditionLockoutTimer>> GetManyCharacterLockoutTimers(
		Database& db, const std::vector<uint32_t>& character_ids,
		const std::string& expedition_name, const std::string& ordered_event_name)
	{
		auto results = db.QueryDatabase(fmt::format(SQL(
			SELECT
				character_id,
				UNIX_TIMESTAMP(expire_time),
				duration,
				event_name,
				from_expedition_uuid
			FROM character_expedition_lockouts
			WHERE
				character_id IN ({})
				AND expire_time > NOW()
				AND expedition_name = '{}'
			ORDER BY
				FIELD(character_id, {}),
				FIELD(event_name, '{}') DESC
		),
			fmt::join(character_ids, ","),
			Strings::Escape(expedition_name),
			fmt::join(character_ids, ","),
			Strings::Escape(ordered_event_name)
		));

		std::unordered_map<uint32_t, std::vector<ExpeditionLockoutTimer>> lockouts;

		for (auto row = results.begin(); row != results.end(); ++row)
		{
			CharacterExpeditionLockoutsTimeStamp entry{};

			int col = 0;
			entry.character_id         = std::strtoul(row[col++], nullptr, 10);
			entry.expire_time          = std::strtoull(row[col++], nullptr, 10);
			entry.duration             = std::strtoul(row[col++], nullptr, 10);
			entry.event_name           = row[col++];
			entry.expedition_name      = expedition_name;
			entry.from_expedition_uuid = row[col++];

			auto lockout = GetExpeditionLockoutTimerFromEntry(std::move(entry));
			lockouts[entry.character_id].emplace_back(std::move(lockout));
		}

		return lockouts;
	}
};

#endif //EQEMU_CHARACTER_EXPEDITION_LOCKOUTS_REPOSITORY_H
