use pixstrip_core::loader::ImageLoader;
use pixstrip_core::types::Dimensions;
use std::path::Path;

fn create_test_jpeg(path: &Path) {
    use image::{RgbImage, ImageFormat};
    let img = RgbImage::from_fn(100, 80, |x, y| {
        image::Rgb([(x % 256) as u8, (y % 256) as u8, 128])
    });
    img.save_with_format(path, ImageFormat::Jpeg).unwrap();
}

fn create_test_png(path: &Path) {
    use image::{RgbaImage, ImageFormat};
    let img = RgbaImage::from_fn(200, 150, |x, y| {
        image::Rgba([(x % 256) as u8, (y % 256) as u8, 100, 255])
    });
    img.save_with_format(path, ImageFormat::Png).unwrap();
}

#[test]
fn load_jpeg_dimensions() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("test.jpg");
    create_test_jpeg(&path);

    let loader = ImageLoader::new();
    let info = loader.load_info(&path).unwrap();
    assert_eq!(info.dimensions, Dimensions { width: 100, height: 80 });
    assert_eq!(info.format, Some(pixstrip_core::types::ImageFormat::Jpeg));
}

#[test]
fn load_png_dimensions() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("test.png");
    create_test_png(&path);

    let loader = ImageLoader::new();
    let info = loader.load_info(&path).unwrap();
    assert_eq!(info.dimensions, Dimensions { width: 200, height: 150 });
    assert_eq!(info.format, Some(pixstrip_core::types::ImageFormat::Png));
}

#[test]
fn load_pixels() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("test.jpg");
    create_test_jpeg(&path);

    let loader = ImageLoader::new();
    let pixels = loader.load_pixels(&path).unwrap();
    assert_eq!(pixels.width(), 100);
    assert_eq!(pixels.height(), 80);
}

#[test]
fn load_nonexistent_file() {
    let loader = ImageLoader::new();
    let result = loader.load_info(Path::new("/tmp/does_not_exist_pixstrip.jpg"));
    assert!(result.is_err());
}

#[test]
fn file_size_bytes() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("test.jpg");
    create_test_jpeg(&path);

    let loader = ImageLoader::new();
    let info = loader.load_info(&path).unwrap();
    assert!(info.file_size > 0);
}
