"""Tests for maker blueprint (client/blueprints/maker.py).

Covers recipe list, recipe creation page, role enforcement, and recipe edit.
"""
import pytest


class TestRecipeList:
    """GET /maker/recipes tests."""

    def test_recipe_list_renders(self, logged_in_client, mock_api_client):
        """Recipe list page renders for Maker role."""
        mock_api_client.get.return_value = {
            "items": [
                {"id": 1, "code": "REC-001", "name": "Recipe A"},
            ],
            "total": 1,
            "pages": 1,
        }

        resp = logged_in_client.get("/maker/recipes")
        assert resp.status_code == 200

    def test_recipe_list_requires_login(self, client):
        """Unauthenticated user is redirected to login."""
        resp = client.get("/maker/recipes", follow_redirects=False)
        assert resp.status_code == 302
        assert "/auth/login" in resp.headers["Location"]


class TestRecipeNew:
    """GET /maker/recipes/new tests."""

    def test_recipe_new_renders(self, logged_in_client, mock_api_client):
        """Recipe creation page renders for Maker role."""
        resp = logged_in_client.get("/maker/recipes/new")
        assert resp.status_code == 200


class TestRoleRequired:
    """Maker role enforcement tests."""

    def test_requires_maker_role(self, flask_app):
        """User without Maker role gets 403 on maker endpoints."""
        with flask_app.test_client() as c:
            with c.session_transaction() as sess:
                sess["api_key"] = "test-key"
                sess["user"] = {
                    "id": 2,
                    "username": "tec_only",
                    "display_name": "Tec Only",
                    "roles": ["MeasurementTec"],
                    "is_admin": False,
                    "language_pref": "en",
                    "theme_pref": "light",
                    "active": True,
                }
                sess["user_id"] = 2
                sess["language"] = "en"
                sess["theme"] = "light"

            resp = c.get("/maker/recipes")
            assert resp.status_code == 403


class TestRecipeEdit:
    """GET /maker/recipes/<id>/edit tests."""

    def test_recipe_edit_renders(self, logged_in_client, mock_api_client):
        """Recipe edit page renders with recipe data."""
        # First call: recipe detail, second call: versions list
        mock_api_client.get.side_effect = [
            {"id": 1, "code": "REC-001", "name": "Recipe A"},
            [{"version_number": 1, "is_current": True}],
        ]

        resp = logged_in_client.get("/maker/recipes/1/edit")
        assert resp.status_code == 200

    def test_recipe_edit_not_found(self, logged_in_client, mock_api_client):
        """Recipe edit with API error redirects to recipe list."""
        mock_api_client.get.return_value = {
            "error": True,
            "detail": "Not found",
        }

        resp = logged_in_client.get("/maker/recipes/999/edit", follow_redirects=False)
        assert resp.status_code == 302
        assert "/maker/recipes" in resp.headers["Location"]
