from decimal import Decimal

from django.test import TestCase
from django.urls import reverse
from django.utils import timezone

from .models import Trip


class TripWorkflowTests(TestCase):
    def setUp(self):
        Trip.objects.create(
            date=timezone.localdate(),
            start_time="09:00",
            end_time="10:00",
            start_location="Office",
            end_location="Client Site",
            business_purpose="Meeting with client about quarterly roadmap",
            trip_type=Trip.TripType.BUSINESS,
            start_odometer=Decimal("100.0"),
            end_odometer=Decimal("112.5"),
        )

    def test_homepage_loads(self):
        response = self.client.get(reverse("home"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Mileage logging that feels ready for tax season")

    def test_trip_list_loads(self):
        response = self.client.get(reverse("trip_list"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Client Site")

    def test_report_csv_exports(self):
        response = self.client.get(reverse("report_export_csv"), {"report_type": "year", "year": timezone.localdate().year})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response["Content-Type"], "text/csv")
        self.assertIn("Miles for this trip", response.content.decode("utf-8"))
