"""
Tests for Routine Steps Extended API
"""

import pytest
import requests
import uuid


@pytest.mark.api
class TestRoutineStepsExtended:
    """Tests for routine_steps_extended.py endpoints."""

    def test_update_step_instructions_success(self, api_base_url, auth_headers, test_routine_uuid, test_step_uuid, sample_routine_data, sample_step_data, db_helper):
        """Test updating step instructions successfully."""
        # Setup: create routine and step
        routine = db_helper.create_routine(sample_routine_data)
        step = db_helper.create_step({**sample_step_data, "routine_id": routine["id"]})
        
        response = requests.put(
            f"{api_base_url}/api/routines/{routine['id']}/steps/{step['id']}/instructions",
            headers=auth_headers,
            json={"instructions": "New instructions for step"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert data["instructions"] == "New instructions for step"

    def test_update_step_instructions_unauthorized(self, api_base_url, test_routine_uuid, test_step_uuid):
        """Test updating step instructions without auth."""
        response = requests.put(
            f"{api_base_url}/api/routines/{test_routine_uuid}/steps/{test_step_uuid}/instructions",
            json={"instructions": "Test"}
        )
        
        assert response.status_code == 401

    def test_update_step_instructions_missing_body(self, api_base_url, auth_headers, sample_routine_data, sample_step_data, db_helper):
        """Test updating step instructions with missing body."""
        routine = db_helper.create_routine(sample_routine_data)
        step = db_helper.create_step({**sample_step_data, "routine_id": routine["id"]})
        
        response = requests.put(
            f"{api_base_url}/api/routines/{routine['id']}/steps/{step['id']}/instructions",
            headers=auth_headers
        )
        
        assert response.status_code == 400

    def test_update_step_type_success(self, api_base_url, auth_headers, sample_routine_data, sample_step_data, db_helper):
        """Test updating step type successfully."""
        routine = db_helper.create_routine(sample_routine_data)
        step = db_helper.create_step({**sample_step_data, "routine_id": routine["id"]})
        
        response = requests.put(
            f"{api_base_url}/api/routines/{routine['id']}/steps/{step['id']}/type",
            headers=auth_headers,
            json={"step_type": "timer"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert data["step_type"] == "timer"

    def test_update_step_type_invalid_type(self, api_base_url, auth_headers, sample_routine_data, sample_step_data, db_helper):
        """Test updating step type with invalid type."""
        routine = db_helper.create_routine(sample_routine_data)
        step = db_helper.create_step({**sample_step_data, "routine_id": routine["id"]})
        
        response = requests.put(
            f"{api_base_url}/api/routines/{routine['id']}/steps/{step['id']}/type",
            headers=auth_headers,
            json={"step_type": "invalid_type"}
        )
        
        assert response.status_code == 400

    def test_update_step_type_all_valid_types(self, api_base_url, auth_headers, sample_routine_data, sample_step_data, db_helper):
        """Test all valid step types."""
        routine = db_helper.create_routine(sample_routine_data)
        step = db_helper.create_step({**sample_step_data, "routine_id": routine["id"]})
        
        valid_types = ["generic", "timer", "checklist", "meditation", "exercise"]
        
        for step_type in valid_types:
            response = requests.put(
                f"{api_base_url}/api/routines/{routine['id']}/steps/{step['id']}/type",
                headers=auth_headers,
                json={"step_type": step_type}
            )
            assert response.status_code == 200

    def test_update_step_media_success(self, api_base_url, auth_headers, sample_routine_data, sample_step_data, db_helper):
        """Test updating step media URL successfully."""
        routine = db_helper.create_routine(sample_routine_data)
        step = db_helper.create_step({**sample_step_data, "routine_id": routine["id"]})
        
        response = requests.put(
            f"{api_base_url}/api/routines/{routine['id']}/steps/{step['id']}/media",
            headers=auth_headers,
            json={"media_url": "https://example.com/audio.mp3"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert data["media_url"] == "https://example.com/audio.mp3"

    def test_update_step_media_empty(self, api_base_url, auth_headers, sample_routine_data, sample_step_data, db_helper):
        """Test updating step media with empty URL."""
        routine = db_helper.create_routine(sample_routine_data)
        step = db_helper.create_step({**sample_step_data, "routine_id": routine["id"]})
        
        response = requests.put(
            f"{api_base_url}/api/routines/{routine['id']}/steps/{step['id']}/media",
            headers=auth_headers,
            json={"media_url": ""}
        )
        
        assert response.status_code == 200

    def test_update_step_not_found(self, api_base_url, auth_headers, test_routine_uuid):
        """Test updating non-existent step."""
        response = requests.put(
            f"{api_base_url}/api/routines/{test_routine_uuid}/steps/{uuid.uuid4()}/instructions",
            headers=auth_headers,
            json={"instructions": "Test"}
        )
        
        assert response.status_code == 404
