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

from .models import SalesWorkspace


class SalesWorkspaceFlowTests(TestCase):
    def test_home_page_loads(self):
        response = self.client.get(reverse("home"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Compact sales workspace")

    def test_create_workspace_redirects_to_detail(self):
        payload = {
            "workspace-customer_name": "Northwind BV",
            "workspace-project_name": "Warehouse fit-out",
            "workspace-project_number": "PRJ-001",
            "workspace-zipcode": "1000 AA",
            "workspace-address": "Harbor Street 10",
            "workspace-city": "Amsterdam",
            "workspace-contact_name": "Lotte",
            "workspace-contact_email": "lotte@example.com",
            "workspace-contact_phone": "+31000000",
            "workspace-opportunity_title": "Prepare first quotation",
            "workspace-estimated_value": "9800",
            "workspace-layout_template": "customer360",
            "workspace-summary": "Test summary",
            "workspace-next_step": "Schedule visit",
            "workspace-next_meeting_at": (timezone.now() + timedelta(days=1)).strftime("%Y-%m-%dT%H:%M"),
        }
        response = self.client.post(reverse("home"), payload)
        workspace = SalesWorkspace.objects.get(customer_name="Northwind BV")
        self.assertRedirects(response, reverse("workspace_detail", args=[workspace.pk]))
        self.assertEqual(workspace.stage, SalesWorkspace.Stage.NEW)

    def test_workspace_detail_shows_panels(self):
        workspace = SalesWorkspace.objects.create(
            customer_name="Northwind BV",
            opportunity_title="Prepare first quotation",
            estimated_value=10000,
        )
        response = self.client.get(reverse("workspace_detail", args=[workspace.pk]))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "Quotations")
        self.assertContains(response, workspace.customer_name)
