package pkg_test

import (
	"archive/tar"
	"bytes"
	"compress/gzip"
	"crypto/sha512"
	"errors"
	"io/fs"
	"net/http"
	"os"
	"testing"
	"testing/fstest"

	"hakurei.app/check"
	"hakurei.app/internal/pkg"
	"hakurei.app/internal/stub"
)

func TestTar(t *testing.T) {
	t.Parallel()

	want := expectsFS{
		".": {Mode: fs.ModeDir | 0500},

		"checksum": {Mode: fs.ModeDir | 0500},
		"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP":                {Mode: fs.ModeDir | 0500},
		"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/check":          {Mode: 0400, Data: []byte{0, 0}},
		"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib":            {Mode: fs.ModeDir | 0500},
		"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/pkgconfig":  {Mode: fs.ModeDir | 0500},
		"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/libedac.so": {Mode: fs.ModeSymlink | 0777, Data: []byte("/proc/nonexistent/libedac.so")},

		"identifier": {Mode: fs.ModeDir | 0500},
		"identifier/HnySzeLQvSBZuTUcvfmLEX_OmH4yJWWH788NxuLuv7kVn8_uPM6Ks4rqFWM2NZJY": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
		"identifier/Zx5ZG9BAwegNT3zQwCySuI2ktCXxNgxirkGLFjW4FW06PtojYVaCdtEw8yuntPLa": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},

		"work": {Mode: fs.ModeDir | 0500},
	}
	wantEncode := pkg.Encode(want.hash())

	wantExpand := expectsFS{
		".": {Mode: fs.ModeDir | 0500},

		"libedac.so": {Mode: fs.ModeSymlink | 0777, Data: []byte("/proc/nonexistent/libedac.so")},
	}
	wantExpandEncode := pkg.Encode(wantExpand.hash())

	checkWithCache(t, []cacheTestCase{
		{"http", 0, nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
			checkTarHTTP(t, base, c, fstest.MapFS{
				".": {Mode: fs.ModeDir | 0700},

				"checksum": {Mode: fs.ModeDir | 0700},
				"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP":                {Mode: fs.ModeDir | 0700},
				"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/check":          {Mode: 0400, Data: []byte{0, 0}},
				"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib":            {Mode: fs.ModeDir | 0700},
				"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/pkgconfig":  {Mode: fs.ModeDir | 0700},
				"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/libedac.so": {Mode: fs.ModeSymlink | 0777, Data: []byte("/proc/nonexistent/libedac.so")},

				"identifier": {Mode: fs.ModeDir | 0700},
				"identifier/HnySzeLQvSBZuTUcvfmLEX_OmH4yJWWH788NxuLuv7kVn8_uPM6Ks4rqFWM2NZJY": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
				"identifier/Zx5ZG9BAwegNT3zQwCySuI2ktCXxNgxirkGLFjW4FW06PtojYVaCdtEw8yuntPLa": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},

				"work": {Mode: fs.ModeDir | 0700},
			}, want)
		}, expectsFS{
			".": {Mode: fs.ModeDir | 0700},

			"checksum":                             {Mode: fs.ModeDir | 0700},
			"checksum/" + wantEncode:               {Mode: fs.ModeDir | 0500},
			"checksum/" + wantEncode + "/checksum": {Mode: fs.ModeDir | 0500},
			"checksum/" + wantEncode + "/checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP":                {Mode: fs.ModeDir | 0500},
			"checksum/" + wantEncode + "/checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/check":          {Mode: 0400, Data: []byte{0, 0}},
			"checksum/" + wantEncode + "/checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib":            {Mode: fs.ModeDir | 0500},
			"checksum/" + wantEncode + "/checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/libedac.so": {Mode: fs.ModeSymlink | 0777, Data: []byte("/proc/nonexistent/libedac.so")},
			"checksum/" + wantEncode + "/checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/pkgconfig":  {Mode: fs.ModeDir | 0500},
			"checksum/" + wantEncode + "/identifier": {Mode: fs.ModeDir | 0500},
			"checksum/" + wantEncode + "/identifier/HnySzeLQvSBZuTUcvfmLEX_OmH4yJWWH788NxuLuv7kVn8_uPM6Ks4rqFWM2NZJY": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
			"checksum/" + wantEncode + "/identifier/Zx5ZG9BAwegNT3zQwCySuI2ktCXxNgxirkGLFjW4FW06PtojYVaCdtEw8yuntPLa": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
			"checksum/" + wantEncode + "/work": {Mode: fs.ModeDir | 0500},

			"identifier": {Mode: fs.ModeDir | 0700},
			"identifier/T68YqeEW5moiwVe4J0wPvjERgyMUd4k_cKKxTcQx6AXqZaQAuuryu-Iv-qxDV6-T": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/" + wantEncode)},
			"identifier/WbWIIUGl6bqDBsjU-UB69JqOWLjG5waiUJBUBGeeRp3V7FBbwaBe3sG1qm7DlPSk": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/" + wantEncode)},

			"substitute": {Mode: fs.ModeDir | 0700},

			"temp": {Mode: fs.ModeDir | 0700},
			"work": {Mode: fs.ModeDir | 0700},
		}},

		{"http expand", 0, nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
			checkTarHTTP(t, base, c, fstest.MapFS{
				".": {Mode: fs.ModeDir | 0700},

				"lib":            {Mode: fs.ModeDir | 0700},
				"lib/libedac.so": {Mode: fs.ModeSymlink | 0777, Data: []byte("/proc/nonexistent/libedac.so")},
			}, wantExpand)
		}, expectsFS{
			".": {Mode: fs.ModeDir | 0700},

			"checksum":                     {Mode: fs.ModeDir | 0700},
			"checksum/" + wantExpandEncode: {Mode: fs.ModeDir | 0500},
			"checksum/" + wantExpandEncode + "/libedac.so": {Mode: fs.ModeSymlink | 0777, Data: []byte("/proc/nonexistent/libedac.so")},

			"identifier": {Mode: fs.ModeDir | 0700},
			"identifier/WbWIIUGl6bqDBsjU-UB69JqOWLjG5waiUJBUBGeeRp3V7FBbwaBe3sG1qm7DlPSk": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/" + wantExpandEncode)},
			"identifier/e3BECw_x_vDyTvj2P48MJVgwpgALeXQ90Ye2Y8gr6SwIPtMup6SR2LVcCTDRGpVi": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/" + wantExpandEncode)},

			"substitute": {Mode: fs.ModeDir | 0700},

			"temp": {Mode: fs.ModeDir | 0700},
			"work": {Mode: fs.ModeDir | 0700},
		}},
	})
}

func checkTarHTTP(
	t *testing.T,
	base *check.Absolute,
	c *pkg.Cache,
	testdataFsys fs.FS,
	want expectsKnown,
) {
	var testdata string
	{
		var buf bytes.Buffer
		w := tar.NewWriter(&buf)
		if err := w.AddFS(testdataFsys); err != nil {
			t.Fatalf("AddFS: error = %v", err)
		}
		if err := w.Close(); err != nil {
			t.Fatalf("Close: error = %v", err)
		}

		var zbuf bytes.Buffer
		gw := gzip.NewWriter(&zbuf)
		if _, err := gw.Write(buf.Bytes()); err != nil {
			t.Fatalf("Write: error = %v", err)
		}
		if err := gw.Close(); err != nil {
			t.Fatalf("Close: error = %v", err)
		}
		testdata = zbuf.String()
	}

	testdataChecksum := func() pkg.Checksum {
		h := sha512.New384()
		h.Write([]byte(testdata))
		return (pkg.Checksum)(h.Sum(nil))
	}()

	var transport http.Transport
	client := http.Client{Transport: &transport}
	transport.RegisterProtocol("file", http.NewFileTransportFS(fstest.MapFS{
		"testdata": {Data: []byte(testdata), Mode: 0400},
	}))

	tarDir := stubArtifact{
		kind:   pkg.KindExec,
		params: []byte("directory containing a single regular file"),
		cure: func(t *pkg.TContext) error {
			work := t.GetWorkDir()
			if err := os.MkdirAll(work.String(), 0700); err != nil {
				return err
			}
			return os.WriteFile(
				work.Append("sample.tar.gz").String(),
				[]byte(testdata),
				0400,
			)
		},
	}
	tarDirMulti := stubArtifact{
		kind:   pkg.KindExec,
		params: []byte("directory containing a multiple entries"),
		cure: func(t *pkg.TContext) error {
			work := t.GetWorkDir()
			if err := os.MkdirAll(work.Append(
				"garbage",
			).String(), 0700); err != nil {
				return err
			}
			return os.WriteFile(
				work.Append("sample.tar.gz").String(),
				[]byte(testdata),
				0400,
			)
		},
	}
	tarDirType := stubArtifact{
		kind:   pkg.KindExec,
		params: []byte("directory containing a symbolic link"),
		cure: func(t *pkg.TContext) error {
			work := t.GetWorkDir()
			if err := os.MkdirAll(work.String(), 0700); err != nil {
				return err
			}
			return os.Symlink(
				work.String(),
				work.Append("sample.tar.gz").String(),
			)
		},
	}
	// destroy these to avoid including it in flatten test case
	defer newDestroyArtifactFunc(&tarDir)(t, base, c)
	defer newDestroyArtifactFunc(&tarDirMulti)(t, base, c)
	defer newDestroyArtifactFunc(&tarDirType)(t, base, c)

	cureMany(t, c, []cureStep{
		{"file", pkg.NewTar(pkg.NewDecompress(pkg.NewHTTPGet(
			&client,
			"file:///testdata",
			testdataChecksum,
		), pkg.Gzip)), ignorePathname, want, pkg.WNew, nil},

		{"directory", pkg.NewTar(pkg.NewDecompress(
			&tarDir,
			pkg.Gzip,
		)), ignorePathname, want, pkg.WNew, nil},

		{"multiple entries", pkg.NewTar(pkg.NewDecompress(
			&tarDirMulti,
			pkg.Gzip,
		)), nil, nil, pkg.WNew, errors.New(
			"input directory does not contain a single regular file",
		)},

		{"bad type", pkg.NewTar(pkg.NewDecompress(
			&tarDirType,
			pkg.Gzip,
		)), nil, nil, pkg.WNew, errors.New(
			"input directory does not contain a single regular file",
		)},

		{"error passthrough", pkg.NewTar(&stubArtifact{
			kind:   pkg.KindExec,
			params: []byte("doomed artifact"),
			cure: func(t *pkg.TContext) error {
				return stub.UniqueError(0xcafe)
			},
		}), nil, nil, pkg.WNew, stub.UniqueError(0xcafe)},
	})
}
