/*******************************************************************************
***  This file is part of Dux Rei Nummariae.                                 ***
***                                                                          ***
***  Copyright (C) 2017, 2018, 2019, 2020, 2021                              ***
***  John Schneiderman <Licensing _AT_ Schneiderman _DOT_ me>                ***
***                                                                          ***
***  This program is free software: you can redistribute it and/or modify it ***
***  under the terms of the GNU Affero General Public License as published   ***
***  by the Free Software Foundation, either version 3 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 Affero General Public License for more details.             ***
***                                                                          ***
***  You should have received a copy of the GNU Affero General Public License***
***  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
*******************************************************************************/
#include <pecunia/Codes.h>
using pecunia::currency::Iso4217Codes;
#include <pecunia/Money.h>
using pecunia::currency::Money;
#include <QDate>
using ::QDate;
#include <QTest>
// using GTEST_MAIN

#include <budgeting/BudgetingErrors.h>
using drn::budgeting::BudgetItemError;
#include <budgeting/BudgetSource.h>
using drn::budgeting::BudgetSource;
#include <budgeting/EventFrequency.h>
using drn::budgeting::EventFrequency;
#include <budgeting/Debt.h>
using drn::budgeting::Debt;
using drn::budgeting::Percentage;


namespace drn
{
namespace budgeting
{
namespace tests
{

struct DebtUT : QObject
{
	Q_OBJECT

private slots:
	// Constructor Tests

	void constructor_Empty_ShouldBeDefaults()
	{
		const BudgetSource expectedSource{};
		const Money expectedOwed{};
		const Money expectedMinimum{};
		const EventFrequency expectedPeriod{EventFrequency::Unknown};
		const Percentage expectedInterest{};
		const Debt l{};
		QCOMPARE(l.source(), expectedSource);
		QCOMPARE(l.amount(), expectedOwed);
		QCOMPARE(l.amount(), expectedMinimum);
		QVERIFY2(l.period() == expectedPeriod, "The periods should be equal.");
		QCOMPARE(l.interest(), expectedInterest);
	}
	void constructor_FullInit_ShouldBeSupplied()
	{
		const BudgetSource expectedSource{"Home Federal Bank"};
		const Money expectedOwed{120000, 0u, Iso4217Codes::USD};
		const Money expectedMinimum{1000, 0u, Iso4217Codes::USD};
		const EventFrequency expectedPeriod{EventFrequency::Monthly};
		const Percentage expectedInterest{3.15};
		const QDate nextOccurOn{9999, 6, 20};
		const Debt l{
			expectedSource,
			expectedMinimum,
			expectedPeriod,
			nextOccurOn,
			expectedOwed,
			expectedInterest
		};
		QCOMPARE(l.source(), expectedSource);
		QCOMPARE(l.borrowed(), expectedOwed);
		QCOMPARE(l.amount(), expectedMinimum);
		QVERIFY2(l.period() == expectedPeriod, "The periods should be equal.");
		QCOMPARE(l.interest(), expectedInterest);
		QCOMPARE(l.nextOccurOn(), nextOccurOn);
	}
	void constructor_InvalidMinimum_LessThanZero_ShouldThrow()
	{
		const BudgetSource source{"Home Federal Bank"};
		const Money owed{120000, 0u, Iso4217Codes::USD};
		const Money minimum{-1000, 0u, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const Percentage interest{3.15};
		QVERIFY_EXCEPTION_THROWN(
			Debt(source, minimum, period, {9999, 6, 20}, owed, interest),
			BudgetItemError
		);
	}
	void constructor_InvalidMinimum_UnknownCurrency_ShouldThrow()
	{
		const BudgetSource source{"Home Federal Bank"};
		const Money owed{120000, 0u, Iso4217Codes::USD};
		const Money minimum{1000, 0u};
		const EventFrequency period{EventFrequency::Monthly};
		const Percentage interest{3.15};
		QVERIFY_EXCEPTION_THROWN(
			Debt(source, minimum, period, {9999, 6, 20}, owed, interest),
			BudgetItemError
		);
	}
	void constructor_InvalidMinimum_MoreThanBorrowed_ShouldThrow()
	{
		const BudgetSource source{"Home Federal Bank"};
		const Money owed{1000, 0u, Iso4217Codes::USD};
		const Money minimum{120000, 0u, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const Percentage interest{3.15};
		QVERIFY_EXCEPTION_THROWN(
			Debt(source, minimum, period, {9999, 6, 20}, owed, interest),
			BudgetItemError
		);
	}
	void constructor_InvalidMinimum_DifferentCurrencyThanBorrowed_ShouldThrow()
	{
		const BudgetSource source{"Home Federal Bank"};
		const Money owed{120000, 0u, Iso4217Codes::USD};
		const Money minimum{10, 0u, Iso4217Codes::PLN};
		const EventFrequency period{EventFrequency::Monthly};
		const Percentage interest{3.15};
		QVERIFY_EXCEPTION_THROWN(
			Debt(source, minimum, period, {9999, 6, 20}, owed, interest),
			BudgetItemError
		);
	}
	void constructor_InvalidInterest_ShouldThrow()
	{
		const BudgetSource source{"Home Federal Bank"};
		const Money owed{120000, 0u, Iso4217Codes::USD};
		const Money minimum{1000, 0u, Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const Percentage interest{-3.15};
		QVERIFY_EXCEPTION_THROWN(
			Debt(source, minimum, period, {9999, 6, 20}, owed, interest),
			BudgetItemError
		);
	}
	void constructor_InvalidBorrowedAmount_ShouldThrow()
	{
		const BudgetSource source{"Home Federal Bank"};
		const Money minimum{Iso4217Codes::USD};
		const EventFrequency period{EventFrequency::Monthly};
		const QDate nextOccurOn{9999, 6, 20};
		const Percentage interest{3.15};
		QVERIFY_EXCEPTION_THROWN(
			Debt(source, minimum, period, nextOccurOn, Money{minimum.code()}, interest),
			BudgetItemError
		);
	}

	//}

};

}}}

QTEST_MAIN(drn::budgeting::tests::DebtUT)
#include "Debt-unit-tests.moc"
