"""Slice A · showcasing P0 hardening tests.

* A7 — ``PitchScoreForm`` enforces score upper-bound (``max_marks``).
* A8 — pitch-deck file validator rejects unsafe uploads.
"""
from __future__ import annotations

import pytest
from django.core.files.uploadedfile import SimpleUploadedFile

from apps.smehub.showcasing.forms import (
    ApplyToEventForm,
    PitchScoreForm,
)


class TestPitchScoreFormBounds:
    """A7 — judges cannot exceed the rubric ceiling."""

    def test_score_within_bounds_valid(self):
        form = PitchScoreForm(
            data={"score": "8", "comments": ""},
            max_marks=10,
        )
        assert form.is_valid(), form.errors

    def test_score_equal_to_max_valid(self):
        form = PitchScoreForm(
            data={"score": "10", "comments": ""},
            max_marks=10,
        )
        assert form.is_valid(), form.errors

    def test_score_above_max_invalid(self):
        form = PitchScoreForm(
            data={"score": "11", "comments": ""},
            max_marks=10,
        )
        assert not form.is_valid()
        assert "score" in form.errors

    def test_score_negative_invalid(self):
        form = PitchScoreForm(
            data={"score": "-1", "comments": ""},
            max_marks=10,
        )
        assert not form.is_valid()
        assert "score" in form.errors


class TestApplyToEventFormPitchDeckValidator:
    """A8 — pitch deck rejects unsafe MIME / oversized uploads."""

    @pytest.mark.django_db
    def test_exe_rejected(self):
        deck = SimpleUploadedFile(
            "malware.exe",
            b"x" * 1024,
            content_type="application/octet-stream",
        )
        form = ApplyToEventForm(
            data={
                "innovation_title": "X",
                "innovation_description": "X",
                "problem_statement": "X",
                "solution_summary": "X",
                "impact_potential": "X",
            },
            files={"pitch_deck": deck},
        )
        # business queryset is empty (no entrepreneur) — that's fine,
        # we're asserting the pitch_deck-level validator fires.
        form.is_valid()
        assert "pitch_deck" in form.errors

    @pytest.mark.django_db
    def test_oversize_pdf_rejected(self):
        deck = SimpleUploadedFile(
            "huge.pdf",
            b"x" * (26 * 1024 * 1024),
            content_type="application/pdf",
        )
        form = ApplyToEventForm(
            data={
                "innovation_title": "X",
                "innovation_description": "X",
                "problem_statement": "X",
                "solution_summary": "X",
                "impact_potential": "X",
            },
            files={"pitch_deck": deck},
        )
        form.is_valid()
        assert "pitch_deck" in form.errors
