package tests

import "core:fmt"
import "core:os"
import "core:strings"
import "core:testing"
import "../src/adapters"
import "../src/core"
import "../src/shared"

join2 :: proc(a, b: string) -> string {
	return fmt.aprintf("%s/%s", a, b)
}

dispose_export_fixture :: proc(layouts: ^[]core.Page_Layout, panel_images: ^map[string]core.Panel_Image) {
	for _, img in panel_images^ {
		delete(img.url)
	}
	delete(panel_images^)
	for l in layouts^ {
		delete(l.panels)
	}
	delete(layouts^)
}

setup_export_fixture :: proc(t: ^testing.T) -> (tmp_dir: string, layouts: []core.Page_Layout, panel_images: map[string]core.Panel_Image) {
	err: os.Error
	tmp_dir, err = os.make_directory_temp("", "comic-export-test-*", context.temp_allocator)
	if err != nil {
		testing.expect(t, false, "failed to create temp dir")
		return "", nil, nil
	}

	src_img := join2(tmp_dir, "src_panel.png")

	// Create a real PNG image using ImageMagick
	img_cmd: [dynamic]string
	append(&img_cmd, "magick")
	append(&img_cmd, "-size")
	append(&img_cmd, "100x100")
	append(&img_cmd, "xc:white")
	append(&img_cmd, src_img)
	defer delete(img_cmd)
	desc := os.Process_Desc{command = img_cmd[:]}
	state, _, _, cerr := os.process_exec(desc, context.temp_allocator)
	if cerr != nil || !state.exited || state.exit_code != 0 {
		// Fallback: write a minimal valid 1x1 PNG
		png_data := []u8{
			0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
			0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
			0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
			0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
			0xde, 0x00, 0x00, 0x00, 0x0c, 0x49, 0x44, 0x41,
			0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff, 0xff, 0xff,
			0x00, 0x05, 0xfe, 0x02, 0xfe, 0xa7, 0x9a, 0x9d,
			0x28, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e,
			0x44, 0xae, 0x42, 0x60, 0x82,
		}
		werr := os.write_entire_file(src_img, string(png_data[:]))
		testing.expect(t, werr == nil, "failed to write fallback png")
	} else {
		testing.expect(t, true, "created test image with ImageMagick")
	}

	panel_id := "panel_1"
	layout_panel := core.Page_Layout_Panel{panel_id = panel_id, panel_number = 1, layout_cell = core.Layout_Cell{x = 0, y = 0, w = 1, h = 1}}
	layout_panels_dyn: [dynamic]core.Page_Layout_Panel
	append(&layout_panels_dyn, layout_panel)

	layout := core.Page_Layout{page_number = 1, pattern_id = "grid-2x2", panels = layout_panels_dyn[:], width = 1000, height = 1400}
	layouts_dyn: [dynamic]core.Page_Layout
	append(&layouts_dyn, layout)

	panel_images = make(map[string]core.Panel_Image)
	panel_images[panel_id] = core.Panel_Image{url = fmt.aprintf("file://%s", src_img), width = 100, height = 100, seed = 1, prompt = ""}
	delete(src_img)

	return tmp_dir, layouts_dyn[:], panel_images
}

@test
export_png_and_cbz_create_files :: proc(t: ^testing.T) {
	tmp_dir, layouts, panel_images := setup_export_fixture(t)
	defer os.remove_all(tmp_dir)
	defer dispose_export_fixture(&layouts, &panel_images)

	png_out := join2(tmp_dir, "out_png.zip")
	defer delete(png_out)
	cbz_out := join2(tmp_dir, "out_cbz.cbz")
	defer delete(cbz_out)

	png_err := adapters.export_comic(png_out, layouts, panel_images, adapters.Export_Options{format = .PNG, page_size = .A4, dpi = 300, quality = 90})
	testing.expect(t, shared.is_ok(png_err), "png export failed")
	testing.expect(t, os.exists(png_out), "png archive output should exist")
	png_data, prerr := os.read_entire_file(png_out, context.temp_allocator)
	testing.expect(t, prerr == nil, "failed reading png zip")
	testing.expect(t, len(png_data) >= 2 && string(png_data[:2]) == "PK", "png export should be a zip archive")

	cbz_err := adapters.export_comic(cbz_out, layouts, panel_images, adapters.Export_Options{format = .CBZ, page_size = .A4, dpi = 300, quality = 90})
	testing.expect(t, shared.is_ok(cbz_err), "cbz export failed")
	testing.expect(t, os.exists(cbz_out), "cbz output should exist")
	cbz_data, crerr := os.read_entire_file(cbz_out, context.temp_allocator)
	testing.expect(t, crerr == nil, "failed reading cbz")
	testing.expect(t, len(cbz_data) >= 2 && string(cbz_data[:2]) == "PK", "cbz should be a zip archive")
}

@test
export_pdf_creates_pdf_file :: proc(t: ^testing.T) {
	tmp_dir, layouts, panel_images := setup_export_fixture(t)
	defer os.remove_all(tmp_dir)
	defer dispose_export_fixture(&layouts, &panel_images)

	pdf_out := join2(tmp_dir, "out.pdf")
	defer delete(pdf_out)
	pdf_err := adapters.export_comic(pdf_out, layouts, panel_images, adapters.Export_Options{format = .PDF, page_size = .A4, dpi = 300, quality = 90})
	testing.expect(t, shared.is_ok(pdf_err), "pdf export failed")
	testing.expect(t, os.exists(pdf_out), "pdf output should exist")

	data, rerr := os.read_entire_file(pdf_out, context.temp_allocator)
	testing.expect(t, rerr == nil, "failed to read pdf output")
	testing.expect(t, strings.has_prefix(string(data), "%PDF-"), "pdf output should start with %PDF-")
}
