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

"""
from datetime import timedelta

from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _

from analysis.forms import TimespanReportForm
from konova.tests.test_views import BaseTestCase


class TimeSpanReportFormTestCase(BaseTestCase):
    def setUp(self) -> None:
        super().setUp()
        eiv = self.create_dummy_intervention()

    def test_init(self):
        form = TimespanReportForm()
        self.assertEqual(form.form_title, str(_("Generate report")))
        self.assertEqual(form.form_caption, str(_("Select a timespan and the desired conservation office") ))
        self.assertEqual(form.action_url, reverse("analysis:reports"))
        self.assertFalse(form.show_cancel_btn)
        self.assertEqual(form.action_btn_label, str(_("Continue")))

    def test_save(self):
        date_from = now().date() - timedelta(days=365)
        date_to = now().date()
        office = self.get_conservation_office_code()
        data = {
            "date_from": date_from,
            "date_to": date_to,
            "conservation_office": office,
        }
        form = TimespanReportForm(data)
        self.assertTrue(form.is_valid(), msg=f"{form.errors}")

        detail_report_url = form.save()
        self.assertEqual(
            detail_report_url,
            reverse("analysis:report-detail", args=(office.id,)) + f"?df={date_from}&dt={date_to}"
        )
