import pikepdf
import pytest

from pdftl.operations.annots_filters import delete_annots
from pdftl.operations.delete import delete_pages
from pdftl.operations.filter import filter_pdf
from pdftl.operations.modify_annots import modify_annots
from pdftl.operations.place import place_content
from pdftl.operations.rebox import crop_or_clip_pages
from pdftl.operations.rotate import rotate_pdf

# Define the cases: (Function, Specs List, Expected Page Count Change)
PARAMS = [
    (rotate_pdf, ["1-endeast"], 0),  # Rotation keeps page count
    (place_content, ["1-end(spin=45)"], 0),  # Spin keeps page count
    (delete_pages, ["2"], -1),  # Deleting 1 page reduces count by 1
    (crop_or_clip_pages, ["1-end(0,0,100,100)"], 0),  # Crop keeps page count
    (filter_pdf, [], 0),  # Filter usually just passes through
    (delete_annots, [], 0),  # Removing annots keeps page count
    # modify_annots usually requires specific args, passing empty list usually does nothing safe
    (modify_annots, [], 0),
]


@pytest.mark.parametrize("func, specs, page_diff", PARAMS)
def test_simple_transform(two_page_pdf, func, specs, page_diff):
    """
    Tests commands that take (pdf, specs) and modify the PDF.
    """
    with pikepdf.open(two_page_pdf) as pdf:
        original_count = len(pdf.pages)

        # Some functions allow empty specs, others might handle it differently.
        # Filter doesn't take specs in your signature: def filter_pdf(pdf)
        if func == filter_pdf:
            _result = func(pdf)
        else:
            _result = func(pdf, specs)

        # Some commands return the PDF, others modify in-place and return None.
        # We check the 'pdf' object itself for validity.
        assert isinstance(pdf, pikepdf.Pdf)
        assert len(pdf.pages) == original_count + page_diff
