// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.

package store

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"testing"
	"time"

	"github.com/ProtonMail/gopenpgp/v2/crypto"
	"github.com/ProtonMail/proton-bridge/v2/internal/store/cache"
	storemocks "github.com/ProtonMail/proton-bridge/v2/internal/store/mocks"
	"github.com/ProtonMail/proton-bridge/v2/pkg/message"
	"github.com/ProtonMail/proton-bridge/v2/pkg/pmapi"
	pmapimocks "github.com/ProtonMail/proton-bridge/v2/pkg/pmapi/mocks"
	tests "github.com/ProtonMail/proton-bridge/v2/test"
	"github.com/golang/mock/gomock"

	"github.com/stretchr/testify/require"
)

const (
	addr1   = "niceaddress@pm.me"
	addrID1 = "niceaddressID"

	addr2   = "jamesandmichalarecool@pm.me"
	addrID2 = "jamesandmichalarecool"

	testPrivateKeyPassword = "apple"
	testPrivateKey         = `-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: OpenPGP.js v0.7.1
Comment: http://openpgpjs.org

xcMGBFRJbc0BCAC0mMLZPDBbtSCWvxwmOfXfJkE2+ssM3ux21LhD/bPiWefE
WSHlCjJ8PqPHy7snSiUuxuj3f9AvXPvg+mjGLBwu1/QsnSP24sl3qD2onl39
vPiLJXUqZs20ZRgnvX70gjkgEzMFBxINiy2MTIG+4RU8QA7y8KzWev0btqKi
MeVa+GLEHhgZ2KPOn4Jv1q4bI9hV0C9NUe2tTXS6/Vv3vbCY7lRR0kbJ65T5
c8CmpqJuASIJNr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KEY BLOCK-----
`
)

var testPrivateKeyRing *crypto.KeyRing

func init() {
	privKey, err := crypto.NewKeyFromArmored(testPrivateKey)
	if err != nil {
		panic(err)
	}

	privKeyUnlocked, err := privKey.Unlock([]byte(testPrivateKeyPassword))
	if err != nil {
		panic(err)
	}

	if testPrivateKeyRing, err = crypto.NewKeyRing(privKeyUnlocked); err != nil {
		panic(err)
	}
}

type mocksForStore struct {
	tb testing.TB

	ctrl           *gomock.Controller
	events         *storemocks.MockListener
	user           *storemocks.MockBridgeUser
	client         *pmapimocks.MockClient
	panicHandler   *storemocks.MockPanicHandler
	changeNotifier *storemocks.MockChangeNotifier
	store          *Store

	tmpDir string
	cache  *Events
}

func initMocks(tb testing.TB) (*mocksForStore, func()) {
	ctrl := gomock.NewController(tb)
	mocks := &mocksForStore{
		tb:             tb,
		ctrl:           ctrl,
		events:         storemocks.NewMockListener(ctrl),
		user:           storemocks.NewMockBridgeUser(ctrl),
		client:         pmapimocks.NewMockClient(ctrl),
		panicHandler:   storemocks.NewMockPanicHandler(ctrl),
		changeNotifier: storemocks.NewMockChangeNotifier(ctrl),
	}

	// Called during clean-up.
	mocks.panicHandler.EXPECT().HandlePanic().AnyTimes()

	var err error
	mocks.tmpDir, err = os.MkdirTemp("", "store-test")
	require.NoError(tb, err)

	cacheFile := filepath.Join(mocks.tmpDir, "cache.json")
	mocks.cache = NewEvents(cacheFile)

	return mocks, func() {
		if err := recover(); err != nil {
			panic(err)
		}
		if mocks.store != nil {
			require.Nil(tb, mocks.store.Close())
		}
		ctrl.Finish()
		require.NoError(tb, os.RemoveAll(mocks.tmpDir))
	}
}

func (mocks *mocksForStore) newStoreNoEvents(t *testing.T, combinedMode bool, msgs ...*pmapi.Message) { //nolint:unparam
	mocks.user.EXPECT().ID().Return("userID").AnyTimes()
	mocks.user.EXPECT().IsConnected().Return(true)
	mocks.user.EXPECT().IsCombinedAddressMode().Return(combinedMode)

	mocks.user.EXPECT().GetClient().AnyTimes().Return(mocks.client)

	testUserKeyring := tests.MakeKeyRing(t)
	mocks.client.EXPECT().GetUserKeyRing().Return(testUserKeyring, nil).AnyTimes()
	mocks.client.EXPECT().Addresses().Return(pmapi.AddressList{
		{ID: addrID1, Email: addr1, Type: pmapi.OriginalAddress, Receive: true},
		{ID: addrID2, Email: addr2, Type: pmapi.AliasAddress, Receive: true},
	})
	mocks.client.EXPECT().ListLabels(gomock.Any()).AnyTimes()
	mocks.client.EXPECT().CountMessages(gomock.Any(), "")

	// Call to get latest event ID and then to process first event.
	eventAfterSyncRequested := make(chan struct{})
	mocks.client.EXPECT().GetEvent(gomock.Any(), "").Return(&pmapi.Event{
		EventID: "firstEventID",
	}, nil)
	mocks.client.EXPECT().GetEvent(gomock.Any(), "firstEventID").DoAndReturn(func(_ context.Context, _ string) (*pmapi.Event, error) {
		close(eventAfterSyncRequested)
		return &pmapi.Event{
			EventID: "latestEventID",
		}, nil
	})

	mocks.client.EXPECT().ListMessages(gomock.Any(), gomock.Any()).Return(msgs, len(msgs), nil).AnyTimes()
	for _, msg := range msgs {
		mocks.client.EXPECT().GetMessage(gomock.Any(), msg.ID).Return(msg, nil).AnyTimes()
	}

	var err error
	mocks.store, err = New(
		nil, // Sentry reporter is not used under unit tests.
		mocks.panicHandler,
		mocks.user,
		mocks.events,
		cache.NewInMemoryCache(1<<20),
		message.NewBuilder(runtime.NumCPU(), runtime.NumCPU()),
		filepath.Join(mocks.tmpDir, "mailbox-test.db"),
		mocks.cache,
	)
	require.NoError(mocks.tb, err)

	require.NoError(mocks.tb, mocks.store.UnlockCache(testUserKeyring))

	// We want to wait until first sync has finished.
	// Checking that event after sync was reuested is not the best way to
	// do the check, because sync could take more time, but sync is going
	// in background and if there is no message to wait for, we don't have
	// anything better.
	select {
	case <-eventAfterSyncRequested:
	case <-time.After(5 * time.Second):
	}
	require.Eventually(mocks.tb, func() bool {
		for _, msg := range msgs {
			_, err := mocks.store.getMessageFromDB(msg.ID)
			if err != nil {
				// To see in test result the latest error for debugging.
				fmt.Println("Sync wait error:", err)
				return false
			}
		}
		return true
	}, 5*time.Second, 10*time.Millisecond)
}
