"""Tests for media processor."""

import json
from pathlib import Path

import pytest
from PIL import Image

from synth_media_compliance_cli.processor import MediaProcessor
from synth_media_compliance_cli.watermark import WatermarkEngine


@pytest.fixture
def temp_dir(tmp_path):
    """Create temporary directory."""
    return tmp_path


@pytest.fixture
def sample_image(temp_dir):
    """Create sample image."""
    img_path = temp_dir / "test.jpg"
    img = Image.new("RGB", (800, 600), color="blue")
    img.save(img_path)
    return img_path


def test_process_image(temp_dir, sample_image):
    """Test image processing."""
    output_path = temp_dir / "output.jpg"

    watermark_engine = WatermarkEngine(text="TEST", opacity=0.5)
    processor = MediaProcessor(watermark_engine=watermark_engine, sign=True)

    result = processor.process_file(
        sample_image, output_path, {"test": "metadata"}
    )

    assert result["success"]
    assert output_path.exists()
    assert result["signature"] is not None


def test_verify_file(temp_dir, sample_image):
    """Test file verification."""
    output_path = temp_dir / "output.jpg"

    watermark_engine = WatermarkEngine()
    processor = MediaProcessor(watermark_engine=watermark_engine, sign=True)

    # Process
    processor.process_file(sample_image, output_path, {})

    # Verify
    verification = processor.verify_file(output_path)

    assert verification["signature"]["valid"]
    assert verification["file_integrity"]["valid"]


def test_invisible_watermark(temp_dir, sample_image):
    """Test invisible watermarking."""
    output_path = temp_dir / "output.jpg"

    watermark_engine = WatermarkEngine(invisible=True)
    processor = MediaProcessor(watermark_engine=watermark_engine, sign=False)

    result = processor.process_file(sample_image, output_path, {})

    assert result["success"]
    assert output_path.exists()

    # Image should look identical but bytes differ
    original = Image.open(sample_image)
    processed = Image.open(output_path)
    assert original.size == processed.size
