from django.test import TestCase, Client
from django.urls import reverse
from uuid import uuid4
from backend.core.models import Organization, Document, Risk, Control, DocumentRiskControl, DocumentTemplate

class DocumentViewTest(TestCase):
    def setUp(self):
        self.client = Client()

        self.organization = Organization.objects.create(
            id=1,
            name="Test Organization",
            email="test@example.com",
            employee_headcount="100-500",
            annual_revenue="$1M-$10M",
            critical_applications="5-10",
            compliance_frameworks=["Ab", "Ba"],
            industry_sector="Technology",
            it_dependency=8,
            data_sensitivity="High",
            network_infrastructure="Cloud-based",
            remote_workforce_percentage="50%",
            third_party_vendor_access="10-20",
            internal_software_development="Moderate",
            geographic_scope="Global",
            customer_base="Enterprise",
            customer_type="B2B",
            product_portfolio="Diverse",
            supplier_base="International",
            it_infrastructure=["Cloud", "On-Premise"],
            intellectual_property=["Patents", "Trademarks"],
            sensitive_data=["PII", "Financial Data"],
            integration_level="Highly Integrated"
        )

        self.document = Document.objects.create(id=uuid4(), organization=self.organization)

        self.risk1 = Risk.objects.create(risk_id=1, risk_name="Risk 1")
        self.risk2 = Risk.objects.create(risk_id=2, risk_name="Risk 2")
        self.control1 = Control.objects.create(id=1, name="Control A")
        self.control2 = Control.objects.create(id=2, name="Control B")

        DocumentRiskControl.objects.create(id=1, document=self.document, risk=self.risk1, control=self.control1, weight=5)
        DocumentRiskControl.objects.create(id=2, document=self.document, risk=self.risk1, control=self.control2, weight=7)
        DocumentRiskControl.objects.create(id=3, document=self.document, risk=self.risk2, control=self.control1, weight=8)

        template_content = """
        - segment_type: "h1"
          content: "{{ document.organization.name }} - Risk Report"
        - segment_type: "body"
          content: "Document ID: {{ document.id }}"
        - segment_type: "body"
          content: "Created at: {{ document.created_at|date:'Y-m-d' }}"
        - segment_type: "h2"
          content: "Risks"
        - segment_type: "body"
          content: |
            {% for item in risks_with_controls %}
            <div class="risk">
              <h3>Risk: {{ item.risk.name }}</h3>
              <div class="controls">
                <h4>Mitigating Controls:</h4>
                {% for control in item.controls %}
                  <div class="control">
                    <span class="name">{{ control.control__name }}</span> -
                    <span class="weight">Weight: {{ control.weight }}</span>
                  </div>
                {% endfor %}
              </div>
            </div>
            {% endfor %}
        """
        self.template = DocumentTemplate.objects.create(id=1, name="Default Template", content=template_content)

    def test_document_view(self):
        url = reverse('core:document', kwargs={'document_id': self.document.id})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, str(self.document.id))
        self.assertContains(response, self.organization.name)
        self.assertContains(response, self.document.created_at.strftime('%Y-%m-%d'))
        self.assertContains(response, self.risk1.risk_name)
        self.assertContains(response, self.control1.name)
        self.assertContains(response, "Weight: 5")