"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 17.08.23

"""
from datetime import timedelta

from django.utils.timezone import now

from analysis.settings import LKOMPVZVO_PUBLISH_DATE
from analysis.utils.report import TimespanReport
from konova.sub_settings.django_settings import DEFAULT_DATE_FORMAT
from konova.tests.test_views import BaseTestCase


class TimeSpanReportTestCase(BaseTestCase):
    def setUp(self) -> None:
        super().setUp()

        today = now().date()
        old_date = LKOMPVZVO_PUBLISH_DATE - timedelta(days=1)

        self.conservation_office = self.get_conservation_office_code()
        self.eiv_old = self.create_dummy_intervention()
        self.kom_old = self.create_dummy_compensation(interv=self.eiv_old)
        self.assertNotEqual(self.compensation.intervention, self.kom_old.intervention)
        self.eiv = self.compensation.intervention
        self.oek_old = self.create_dummy_eco_account()

        self.eiv_old.responsible.conservation_office = self.conservation_office
        self.eiv_old.legal.binding_date = old_date
        self.eiv_old.legal.registration_date = old_date

        self.eiv.responsible.conservation_office = self.conservation_office
        self.eiv.legal.binding_date = today
        self.eiv.legal.registration_date = today

        self.eco_account.responsible.conservation_office = self.conservation_office
        self.eco_account.legal.registration_date = today
        self.eco_account.legal.binding_date = today

        self.oek_old.responsible.conservation_office = self.conservation_office
        self.oek_old.legal.registration_date = old_date
        self.oek_old.legal.binding_date = old_date

        self.eiv.legal.save()
        self.eiv.responsible.save()

        self.eiv_old.legal.save()
        self.eiv_old.responsible.save()

        self.eco_account.legal.save()
        self.eco_account.responsible.save()

        self.oek_old.legal.save()
        self.oek_old.responsible.save()

        self.deduction.account = self.eco_account
        self.deduction.intervention = self.eiv
        self.deduction.save()

    def test_init(self):
        date_from = now().date() - timedelta(days=365)
        date_to = now().date()
        report = TimespanReport(self.conservation_office.id, date_from, date_to)

        self.assertEqual(report.office_id, self.conservation_office.id)
        self.assertEqual(report.date_from, date_from)
        self.assertEqual(report.date_to, date_to)

        self.assertIsNotNone(report.intervention_report)
        self.assertIsNotNone(report.compensation_report)
        self.assertIsNotNone(report.eco_account_report)
        self.assertIsNotNone(report.old_data_report)

        self.assertEqual(report.excel_map["date_from"], date_from.strftime(DEFAULT_DATE_FORMAT))
        self.assertEqual(report.excel_map["date_to"], date_to.strftime(DEFAULT_DATE_FORMAT))

        self.assertEqual(report.old_data_report.queryset_intervention_count, 1)
        self.assertEqual(report.old_data_report.queryset_intervention_recorded_count, 0)
        self.assertEqual(report.old_data_report.queryset_comps_count, 1)
        self.assertEqual(report.old_data_report.queryset_acc_count, 1)
        self.assertEqual(report.old_data_report.queryset_acc_recorded_count, 0)

        self.assertEqual(report.intervention_report.queryset_count, 1)
        self.assertEqual(report.intervention_report.queryset_checked_count, 0)
        self.assertEqual(report.intervention_report.queryset_recorded_count, 0)

        self.assertEqual(report.compensation_report.queryset_count, 1)
        self.assertEqual(report.compensation_report.queryset_checked_count, 0)
        self.assertEqual(report.compensation_report.queryset_recorded_count, 0)

        self.assertEqual(report.eco_account_report.queryset_count, 1)
        self.assertEqual(report.eco_account_report.queryset_recorded_count, 0)
        self.assertEqual(report.eco_account_report.queryset_deductions_count, 1)
        self.assertEqual(report.eco_account_report.queryset_deductions_recorded_count, 0)
