"""Tests for table_to_image core functionality."""
import tempfile
from pathlib import Path
import pandas as pd
from PIL import Image

from table_to_image import DataFrameToImage, render


def test_dataframe_to_image_init():
    """Test DataFrameToImage initialization with default values."""
    converter = DataFrameToImage()
    assert converter.font_size == 14
    assert converter.header_bg_color == (70, 130, 180)
    assert converter.header_text_color == (255, 255, 255)
    assert converter.cell_bg_color == (255, 255, 255)
    assert converter.cell_text_color == (0, 0, 0)
    assert converter.margin == 10


def test_dataframe_to_image_custom_init():
    """Test DataFrameToImage initialization with custom values."""
    converter = DataFrameToImage(
        font_size=20,
        header_bg_color=(255, 0, 0),
        cell_bg_color=(200, 200, 200),
        margin=20
    )
    assert converter.font_size == 20
    assert converter.header_bg_color == (255, 0, 0)
    assert converter.margin == 20


def test_render_dataframe():
    """Test render function creates a valid image."""
    df = pd.DataFrame({
        'Name': ['Alice', 'Bob'],
        'Age': [25, 30],
        'City': ['NYC', 'LA']
    })

    with tempfile.TemporaryDirectory() as tmpdir:
        output_path = Path(tmpdir) / "test.png"
        render(df, output_path)

        # Verify file was created
        assert output_path.exists()

        # Verify it's a valid image
        img = Image.open(output_path)
        assert img.format == 'PNG'
        assert img.size[0] > 0
        assert img.size[1] > 0


def test_dataframe_to_image_convert():
    """Test DataFrameToImage.convert method."""
    df = pd.DataFrame({
        'Column A': ['Value 1', 'Value 2'],
        'Column B': [100, 200]
    })

    with tempfile.TemporaryDirectory() as tmpdir:
        output_path = Path(tmpdir) / "convert_test.png"
        converter = DataFrameToImage()
        converter.convert(df, output_path)

        assert output_path.exists()
        img = Image.open(output_path)
        assert img.format == 'PNG'


def test_render_with_custom_colors():
    """Test render function with custom colors."""
    df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})

    with tempfile.TemporaryDirectory() as tmpdir:
        output_path = Path(tmpdir) / "custom_colors.png"
        render(
            df,
            output_path,
            font_size=16,
            header_bg_color=(0, 128, 0),
            header_text_color=(255, 255, 0)
        )

        assert output_path.exists()
