package jobs

import (
	"archive/zip"
	"bytes"
	"encoding/json"
	"image"
	"image/color"
	"image/jpeg"
	"image/png"
	"io"
	"log/slog"
	"mime/multipart"
	"net/http/httptest"
	"net/textproto"
	"strings"
	"testing"

	"warpbox.dev/backend/libs/services"
)

func TestGenerateMissingThumbnailsForBox(t *testing.T) {
	service := newThumbnailTestUploadService(t)
	result := createThumbnailTestBox(t, service)
	box, err := service.GetBox(result.BoxID)
	if err != nil {
		t.Fatalf("GetBox returned error: %v", err)
	}
	if box.Files[0].Thumbnail != "" {
		t.Fatalf("thumbnail should start empty")
	}

	jobResult, err := generateMissingThumbnailsForBox(service, slog.New(slog.NewTextHandler(io.Discard, nil)), box)
	if err != nil {
		t.Fatalf("generateMissingThumbnailsForBox returned error: %v", err)
	}
	if jobResult.Generated != 1 || jobResult.Failed != 0 {
		t.Fatalf("job result = %+v, want 1 generated and 0 failed", jobResult)
	}

	updated, err := service.GetBox(result.BoxID)
	if err != nil {
		t.Fatalf("GetBox after thumbnail returned error: %v", err)
	}
	if updated.Files[0].Thumbnail == "" {
		t.Fatalf("thumbnail was not saved to box metadata")
	}
	if service.ThumbnailPath(updated, updated.Files[0]) == "" {
		t.Fatalf("thumbnail path was empty")
	}
}

func TestGenerateMissingThumbnailsForTroubleBoxSkipsWork(t *testing.T) {
	service := newThumbnailTestUploadService(t)
	result := createThumbnailTestBox(t, service)
	box, err := service.GetBox(result.BoxID)
	if err != nil {
		t.Fatalf("GetBox returned error: %v", err)
	}
	box.Trouble = true
	box.TroubleReason = "storage backend failed"
	if err := service.SaveBox(box); err != nil {
		t.Fatalf("SaveBox returned error: %v", err)
	}

	jobResult, err := generateMissingThumbnailsForBox(service, slog.New(slog.NewTextHandler(io.Discard, nil)), box)
	if err != nil {
		t.Fatalf("generateMissingThumbnailsForBox returned error: %v", err)
	}
	if jobResult != (ThumbnailJobResult{}) {
		t.Fatalf("job result = %+v, want no work for trouble box", jobResult)
	}

	updated, err := service.GetBox(result.BoxID)
	if err != nil {
		t.Fatalf("GetBox after job returned error: %v", err)
	}
	if updated.Files[0].Thumbnail != "" {
		t.Fatalf("thumbnail was generated for trouble box: %+v", updated.Files[0])
	}
}

func TestCreateTextThumbnailRendersMarkdownAsJPEG(t *testing.T) {
	data, err := createTextThumbnail(services.File{
		Name:        "notes.md",
		ContentType: "text/markdown",
	}, strings.NewReader("# Meeting notes\n\n```go\nfunc main() {}\n```\n\nA rendered Markdown preview."))
	if err != nil {
		t.Fatalf("createTextThumbnail returned error: %v", err)
	}

	img, err := jpeg.Decode(bytes.NewReader(data))
	if err != nil {
		t.Fatalf("jpeg.Decode returned error: %v", err)
	}
	if img.Bounds().Dx() != 360 || img.Bounds().Dy() != 240 {
		t.Fatalf("thumbnail size = %dx%d, want 360x240", img.Bounds().Dx(), img.Bounds().Dy())
	}
}

func TestNeedsThumbnailIncludesCodeTextFiles(t *testing.T) {
	if !needsThumbnail(services.File{Name: "main.go", ContentType: "text/plain"}) {
		t.Fatalf("Go source file should get a text thumbnail")
	}
}

func TestUsableVideoFrameRejectsBlackFrame(t *testing.T) {
	var dark bytes.Buffer
	if err := jpeg.Encode(&dark, solidTestImage(color.RGBA{A: 255}), nil); err != nil {
		t.Fatalf("jpeg.Encode dark returned error: %v", err)
	}
	if usableVideoFrame(dark.Bytes()) {
		t.Fatalf("black video frame should not be usable")
	}

	var bright bytes.Buffer
	if err := jpeg.Encode(&bright, solidTestImage(color.RGBA{R: 180, G: 80, B: 40, A: 255}), nil); err != nil {
		t.Fatalf("jpeg.Encode bright returned error: %v", err)
	}
	if !usableVideoFrame(bright.Bytes()) {
		t.Fatalf("bright video frame should be usable")
	}
}

func TestRenderVideoScenesThumbnailReturnsLargeJPEG(t *testing.T) {
	data := renderVideoScenesThumbnail(
		services.File{Name: "clip.mp4", ContentType: "video/mp4"},
		videoInfo{Codec: "h264", Width: 1920, Height: 1080, Duration: 125, FrameRate: "24.00 fps"},
		[]videoSceneFrame{
			{Timestamp: "00:00:10", Image: solidTestImage(color.RGBA{R: 140, G: 40, B: 80, A: 255})},
			{Timestamp: "00:00:35", Image: solidTestImage(color.RGBA{R: 40, G: 120, B: 150, A: 255})},
		},
	)
	img, err := jpeg.Decode(bytes.NewReader(data))
	if err != nil {
		t.Fatalf("jpeg.Decode returned error: %v", err)
	}
	if img.Bounds().Dx() != 1200 || img.Bounds().Dy() != 630 {
		t.Fatalf("scene preview size = %dx%d, want 1200x630", img.Bounds().Dx(), img.Bounds().Dy())
	}
}

func TestCreateArchiveListingRendersZipTree(t *testing.T) {
	var archive bytes.Buffer
	writer := zip.NewWriter(&archive)
	addZipTestFile(t, writer, "docs/readme.md", "hello")
	addZipTestFile(t, writer, "src/main.go", "package main\n")
	if err := writer.Close(); err != nil {
		t.Fatalf("zip.Close returned error: %v", err)
	}

	data, err := createArchiveListing(services.File{Name: "bundle.zip", ContentType: "application/zip"}, bytes.NewReader(archive.Bytes()))
	if err != nil {
		t.Fatalf("createArchiveListing returned error: %v", err)
	}
	var listing archiveListingData
	if err := json.Unmarshal(data, &listing); err != nil {
		t.Fatalf("json.Unmarshal returned error: %v\n%s", err, string(data))
	}
	if listing.Name != "bundle.zip" || listing.FileCount != 2 || listing.FolderCount != 2 {
		t.Fatalf("archive listing metadata = %+v", listing)
	}
	if listing.Root == nil || len(listing.Root.Items) != 2 {
		t.Fatalf("archive listing root = %+v", listing.Root)
	}
	if listing.Root.Items[0].Name != "docs" || listing.Root.Items[0].Icon != "folder" {
		t.Fatalf("first archive folder = %+v", listing.Root.Items[0])
	}
	if listing.Root.Items[0].Items[0].Name != "readme.md" || listing.Root.Items[0].Items[0].Icon != "txt" {
		t.Fatalf("markdown archive file = %+v", listing.Root.Items[0].Items[0])
	}
	if listing.Root.Items[1].Items[0].Name != "main.go" || listing.Root.Items[1].Items[0].Icon != "code" {
		t.Fatalf("go archive file = %+v", listing.Root.Items[1].Items[0])
	}
}

func addZipTestFile(t *testing.T, writer *zip.Writer, name, body string) {
	t.Helper()
	file, err := writer.Create(name)
	if err != nil {
		t.Fatalf("zip.Create returned error: %v", err)
	}
	if _, err := file.Write([]byte(body)); err != nil {
		t.Fatalf("zip file write returned error: %v", err)
	}
}

func solidTestImage(c color.Color) image.Image {
	img := image.NewRGBA(image.Rect(0, 0, 32, 24))
	for y := 0; y < img.Bounds().Dy(); y++ {
		for x := 0; x < img.Bounds().Dx(); x++ {
			img.Set(x, y, c)
		}
	}
	return img
}

func newThumbnailTestUploadService(t *testing.T) *services.UploadService {
	t.Helper()
	service, err := services.NewUploadService(1024*1024, t.TempDir(), "http://example.test", slog.New(slog.NewTextHandler(io.Discard, nil)))
	if err != nil {
		t.Fatalf("NewUploadService returned error: %v", err)
	}
	t.Cleanup(func() {
		if err := service.Close(); err != nil {
			t.Fatalf("Close returned error: %v", err)
		}
	})
	return service
}

func createThumbnailTestBox(t *testing.T, service *services.UploadService) services.UploadResult {
	t.Helper()
	result, err := service.CreateBox(thumbnailTestFileHeaders(t), services.UploadOptions{MaxDays: 1})
	if err != nil {
		t.Fatalf("CreateBox returned error: %v", err)
	}
	return result
}

func thumbnailTestFileHeaders(t *testing.T) []*multipart.FileHeader {
	t.Helper()
	var imageData bytes.Buffer
	img := image.NewRGBA(image.Rect(0, 0, 2, 2))
	img.Set(0, 0, color.RGBA{R: 255, A: 255})
	if err := png.Encode(&imageData, img); err != nil {
		t.Fatalf("png.Encode returned error: %v", err)
	}

	var payload bytes.Buffer
	writer := multipart.NewWriter(&payload)
	header := make(textproto.MIMEHeader)
	header.Set("Content-Disposition", `form-data; name="file"; filename="thumb.png"`)
	header.Set("Content-Type", "image/png")
	part, err := writer.CreatePart(header)
	if err != nil {
		t.Fatalf("CreateFormFile returned error: %v", err)
	}
	if _, err := part.Write(imageData.Bytes()); err != nil {
		t.Fatalf("part.Write returned error: %v", err)
	}
	if err := writer.Close(); err != nil {
		t.Fatalf("writer.Close returned error: %v", err)
	}

	request := httptest.NewRequest("POST", "/upload", &payload)
	request.Header.Set("Content-Type", writer.FormDataContentType())
	if err := request.ParseMultipartForm(1024 * 1024); err != nil {
		t.Fatalf("ParseMultipartForm returned error: %v", err)
	}
	return request.MultipartForm.File["file"]
}
