#**************************************************************************
#   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
#                                                                         *
#   This program is free software; you can redistribute it and/or modify  *
#   it under the terms of the GNU General Public License as published by  *
#   the Free Software Foundation; either version 2 of the License, or     *
#   (at your option) any later version.                                   *
#                                                                         *
#   This program is distributed in the hope that it will be useful,       *
#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
#   GNU General Public License for more details.                          *
#                                                                         *
#   You should have received a copy of the GNU General Public License     *
#   along with this program; if not, write to the                         *
#   Free Software Foundation, Inc.,                                       *
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
#**************************************************************************/

use strict;
use warnings;
use feature ':5.10';

use MooseX::Declare;
use NDIRC::Dispatcher;


command quote => {
	help => q(syntax .quote [number|search term] | if number/search term isn't given, then a random quote is used),
	alias => q(lastquote),
}, class extends NDIRC::Command {
	method execute($c,$msg) {
		my $dbh = $c->model;
		my $quote;
		if ($msg =~ /(\d+)/) {
			my $quote = $dbh->selectrow_hashref(q{SELECT qid,quote FROM quotes WHERE qid = ?},  undef, $1);
			if ($quote) {
				$c->reply("Quote <b>$quote->{qid}</b>: $quote->{quote}");
			} else {
				$c->reply("No quote with id <b>$1</b>");
			}
		} elsif ($msg =~ /^\s*(.+?)\s*$/) {
			say $1;
			my $quote = $dbh->selectrow_hashref(q{
SELECT qid,quote FROM quotes
WHERE quote ILIKE '%' || $1 || '%' ORDER BY random() limit 1
				},  undef, $1);
			if ($quote) {
				$c->reply("Quote <b>$quote->{qid}</b>: $quote->{quote}");
			} else {
				$c->reply("No quote matching <b>$1</b>");
			}
		} else {
			say "default";
			my $quote = $dbh->selectrow_hashref(q{
SELECT qid,quote FROM quotes tablesample system(5) ORDER BY random() LIMIT 1
				});
			$c->reply("Quote <b>$quote->{qid}</b>: $quote->{quote}");
		}
	}
};

command addquote => {
	help => q(syntax .addquote quote),
}, class extends NDIRC::Command {
	method execute($c,$msg) {

		my ($quote) = $msg =~ /^\s*(.+?)\s*$/ or die 'ARGS';

		my $dbh = $c->model;
		my ($qid) = $dbh->selectrow_array(q{
INSERT INTO quotes (uid,quote) VALUES($1,$2) RETURNING qid
			},  undef, $c->uid, $quote);
		$c->reply("Quote <b>$qid</b> '$quote' added");
	}
};

command delquote => {
	help => q(syntax: .delquote number),
	acl => q(irc_delquote),
}, class extends NDIRC::Command {
	method execute($c,$msg) {
		my ($n) = $msg =~ /(\d+)/ or die 'ARGS';
		my $dbh = $c->model;
		my $quote = $dbh->selectrow_hashref(q{
WITH q AS (DELETE FROM quotes WHERE qid = $1 RETURNING qid,time,uid,quote)
INSERT INTO removed_quotes (qid,time,uid,quote,removed_by)
	SELECT *, $2 AS removed_by FROM q RETURNING quote;
			},  undef, $n, $c->uid);
		if ($quote){
			$c->reply("Quote <b>$n</b> {$quote->{quote}} removed");
		}else{
			$c->reply("No such quote.");
		}
	}
};

1;
