// Anope IRC Services <https://www.anope.org/>
//
// Copyright (C) 2003-2026 Anope Contributors
//
// Anope is free software. You can use, modify, and/or distribute it under the
// terms of version 2 of the GNU General Public License. See docs/LICENSE.txt
// for the complete terms of this license and docs/AUTHORS.txt for a list of
// contributors.
//
// Based on the original code of Epona by Lara
// Based on the original code of Services by Andy Church
//
// SPDX-License-Identifier: GPL-2.0-only

#include "module.h"

class MemoDelCallback final
	: public NumberList
{
	CommandSource &source;
	Command *cmd;
	ChannelInfo *ci;
	MemoInfo *mi;
public:
	MemoDelCallback(CommandSource &_source, Command *c, ChannelInfo *_ci, MemoInfo *_mi, const Anope::string &list) : NumberList(list, true), source(_source), cmd(c), ci(_ci), mi(_mi)
	{
	}

	void HandleNumber(unsigned number) override
	{
		if (!number || number > mi->memos->size())
			return;

		FOREACH_MOD(OnMemoDel, (ci ? ci->name : source.nc->display, mi, mi->GetMemo(number - 1)));

		mi->Del(number - 1);
		source.Reply(_("Memo %d has been deleted."), number);
		if (ci)
			Log(LOG_COMMAND, source, cmd, ci) << "on memo " << number;
	}
};

class CommandMSDel final
	: public Command
{
public:
	CommandMSDel(Module *creator) : Command(creator, "memoserv/del", 0, 2)
	{
		this->SetDesc(_("Delete a memo or memos"));
		this->SetSyntax(_("[\037channel\037] {\037num\037 | \037list\037 | LAST | ALL}"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		if (Anope::ReadOnly)
		{
			source.Reply(READ_ONLY_MODE);
			return;
		}

		MemoInfo *mi;
		ChannelInfo *ci = NULL;
		Anope::string numstr = !params.empty() ? params[0] : "", chan;

		if (!numstr.empty() && numstr[0] == '#')
		{
			chan = numstr;
			numstr = params.size() > 1 ? params[1] : "";

			ci = ChannelInfo::Find(chan);
			if (!ci)
			{
				source.Reply(CHAN_X_NOT_REGISTERED, chan.c_str());
				return;
			}
			else if (!source.AccessFor(ci).HasPriv("MEMO"))
			{
				source.Reply(ACCESS_DENIED);
				return;
			}
			mi = &ci->memos;
		}
		else
			mi = &source.nc->memos;
		if (numstr.empty() || (!isdigit(numstr[0]) && !numstr.equals_ci("ALL") && !numstr.equals_ci("LAST")))
			this->OnSyntaxError(source, numstr);
		else if (mi->memos->empty())
		{
			if (!chan.empty())
				source.Reply(MEMO_X_HAS_NO_MEMOS, chan.c_str());
			else
				source.Reply(MEMO_HAVE_NO_MEMOS);
		}
		else
		{
			if (isdigit(numstr[0]))
			{
				MemoDelCallback list(source, this, ci, mi, numstr);
				list.Process();
			}
			else if (numstr.equals_ci("LAST"))
			{
				/* Delete last memo. */
				FOREACH_MOD(OnMemoDel, (ci ? ci->name : source.nc->display, mi, mi->GetMemo(mi->memos->size() - 1)));
				mi->Del(mi->memos->size() - 1);
				source.Reply(_("Memo %zu has been deleted."), mi->memos->size() + 1);
				if (ci)
					Log(LOG_COMMAND, source, this, ci) << "on LAST memo";
			}
			else
			{
				/* Delete all memos. */
				for (unsigned i = mi->memos->size(); i > 0; --i)
				{
					FOREACH_MOD(OnMemoDel, (ci ? ci->name : source.nc->display, mi, mi->GetMemo(i)));
					mi->Del(i - 1);
				}
				if (!chan.empty())
				{
					source.Reply(_("All memos for channel %s have been deleted."), chan.c_str());
					Log(LOG_COMMAND, source, this, ci) << "on ALL memos";
				}
				else
					source.Reply(_("All of your memos have been deleted."));
			}
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		this->SendSyntax(source);
		source.Reply(" ");
		source.Reply(_(
			"Deletes the specified memo or memos. You can supply "
			"multiple memo numbers or ranges of numbers instead of a "
			"single number, as in the second example below."
		));

		ExampleWrapper examples;
		examples.AddEntry("ALL", _(
			"Deletes all of your memos."
		));
		examples.AddEntry("LAST", _(
			"Deletes your last memo."
		));
		examples.AddEntry("1", _(
			"Deletes your first memo."
		));
		examples.AddEntry("2-5,7-9", _(
			"Deletes memos numbered 2 through 5 and 7 through 9."
		));
		examples.SendTo(source);

		return true;
	}
};

class MSDel final
	: public Module
{
	CommandMSDel commandmsdel;

public:
	MSDel(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
		commandmsdel(this)
	{

	}
};

MODULE_INIT(MSDel)
