"""Smoke tests for course-transfer's underlying Canvas API helper.

The course-transfer skill is doc-only (no dedicated script). It calls
canvas_api.create_content_migration() to issue Canvas's course_copy_importer
migration. Tests verify the helper exists with the expected signature and
forwards the right payload to Canvas. Cross-instance transfers go through
Canvas's own UI per skill doc — nothing to test here.
"""
import inspect
import os
import sys
from unittest.mock import MagicMock, patch

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "scripts"))


def test_create_content_migration_exists_with_course_copy_signature():
    import canvas_api

    assert hasattr(canvas_api, "create_content_migration")
    sig = inspect.signature(canvas_api.create_content_migration)
    params = list(sig.parameters)
    # Must accept (config, source_course_id, ...). Optional `select` kwarg
    # supports partial-content migrations.
    assert params[:2] == ["config", "source_course_id"]
    assert "select" in params


def test_create_content_migration_posts_course_copy_importer():
    import canvas_api

    fake_response = MagicMock()
    fake_response.status_code = 200
    fake_response.json.return_value = {"id": 42, "workflow_state": "queued"}

    config = {
        "course_id": "111",
        "domain": "canvas.test.edu",
        "token": "T",
        "headers": {"Authorization": "Bearer T"},
        "course_url": "https://canvas.test.edu/api/v1/courses/111",
    }

    with patch.object(canvas_api, "_request_with_retry", return_value=fake_response) as mock_req:
        result = canvas_api.create_content_migration(config, source_course_id=222)

    assert result is not None
    assert mock_req.called

    # Inspect the POST payload — must use course_copy_importer.
    args, kwargs = mock_req.call_args
    payload = kwargs.get("json", {})
    assert payload.get("migration_type") == "course_copy_importer", (
        f"Expected migration_type='course_copy_importer', got {payload.get('migration_type')!r}"
    )
    settings = payload.get("settings", {})
    # Canvas accepts source_course_id as a string in the settings dict; the
    # helper stringifies it before send.
    assert str(settings.get("source_course_id")) == "222" or str(payload.get("source_course_id")) == "222"


def test_course_transfer_skill_docs_no_longer_reference_imscc_builder():
    """imscc_builder.py was deleted in Phase 3; the skill doc must not point
    users at it. (Cross-instance copy now directs to Canvas's native UI.)"""
    skill_path = os.path.join(HERE, "..", "skills", "course-transfer", "SKILL.md")
    with open(skill_path, encoding="utf-8") as f:
        content = f.read()
    assert "imscc_builder" not in content, (
        "course-transfer SKILL.md still references imscc_builder.py — should be removed"
    )
