//go:build integration
// +build integration

/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH

This program 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.

This program 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 this program.  If not, see <http://www.gnu.org/licenses/>
*/

package migrator

import (
	"log"
	"path"
	"reflect"
	"testing"

	"github.com/cgrates/cgrates/config"
	"github.com/cgrates/cgrates/engine"
	"github.com/cgrates/cgrates/utils"
)

var (
	tpTimPathIn   string
	tpTimPathOut  string
	tpTimCfgIn    *config.CGRConfig
	tpTimCfgOut   *config.CGRConfig
	tpTimMigrator *Migrator
	tpTimings     []*utils.ApierTPTiming
)

var sTestsTpTimIT = []func(t *testing.T){
	testTpTimITConnect,
	testTpTimITFlush,
	testTpTimITPopulate,
	testTpTimITMove,
	testTpTimITCheckData,
}

func TestTpTimMove(t *testing.T) {
	for _, stest := range sTestsTpTimIT {
		t.Run("TestTpTimMove", stest)
	}
	tpTimMigrator.Close()
}

func testTpTimITConnect(t *testing.T) {
	var err error
	tpTimPathIn = path.Join(*utils.DataDir, "conf", "samples", "tutmongo")
	tpTimCfgIn, err = config.NewCGRConfigFromPath(tpTimPathIn)
	if err != nil {
		t.Fatal(err)
	}
	tpTimPathOut = path.Join(*utils.DataDir, "conf", "samples", "tutmysql")
	tpTimCfgOut, err = config.NewCGRConfigFromPath(tpTimPathOut)
	if err != nil {
		t.Fatal(err)
	}
	storDBIn, err := NewMigratorStorDB(tpTimCfgIn.StorDbCfg().Type,
		tpTimCfgIn.StorDbCfg().Host, tpTimCfgIn.StorDbCfg().Port,
		tpTimCfgIn.StorDbCfg().Name, tpTimCfgIn.StorDbCfg().User,
		tpTimCfgIn.StorDbCfg().Password, tpTimCfgIn.GeneralCfg().DBDataEncoding,
		tpTimCfgIn.StorDbCfg().StringIndexedFields, tpTimCfgIn.StorDbCfg().PrefixIndexedFields,
		tpTimCfgIn.StorDbCfg().Opts, nil)
	if err != nil {
		log.Fatal(err)
	}
	storDBOut, err := NewMigratorStorDB(tpTimCfgOut.StorDbCfg().Type,
		tpTimCfgOut.StorDbCfg().Host, tpTimCfgOut.StorDbCfg().Port,
		tpTimCfgOut.StorDbCfg().Name, tpTimCfgOut.StorDbCfg().User,
		tpTimCfgOut.StorDbCfg().Password, tpTimCfgOut.GeneralCfg().DBDataEncoding,
		tpTimCfgIn.StorDbCfg().StringIndexedFields, tpTimCfgIn.StorDbCfg().PrefixIndexedFields,
		tpTimCfgOut.StorDbCfg().Opts, nil)
	if err != nil {
		log.Fatal(err)
	}
	tpTimMigrator, err = NewMigrator(nil, nil, storDBIn, storDBOut, false, false, false, false)
	if err != nil {
		log.Fatal(err)
	}
}

func testTpTimITFlush(t *testing.T) {
	if err := tpTimMigrator.storDBIn.StorDB().Flush(
		path.Join(tpTimCfgIn.DataFolderPath, "storage", dbPath(tpTimCfgIn.StorDbCfg().Type))); err != nil {
		t.Error(err)
	}

	if err := tpTimMigrator.storDBOut.StorDB().Flush(
		path.Join(tpTimCfgOut.DataFolderPath, "storage", dbPath(tpTimCfgOut.StorDbCfg().Type))); err != nil {
		t.Error(err)
	}
}

func testTpTimITPopulate(t *testing.T) {
	tpTimings = []*utils.ApierTPTiming{
		{
			TPid:      "TPT1",
			ID:        "Timing",
			Years:     "2017",
			Months:    "05",
			MonthDays: "01",
			WeekDays:  "1",
			Time:      "15:00:00Z",
		},
	}
	if err := tpTimMigrator.storDBIn.StorDB().SetTPTimings(tpTimings); err != nil {
		t.Error("Error when setting TpTimings ", err.Error())
	}
	currentVersion := engine.CurrentStorDBVersions()
	err := tpTimMigrator.storDBIn.StorDB().SetVersions(currentVersion, false)
	if err != nil {
		t.Error("Error when setting version for TpTimings ", err.Error())
	}
}

func testTpTimITMove(t *testing.T) {
	_, err := tpTimMigrator.Migrate([]string{utils.MetaTpTimings})
	if err != nil {
		t.Error("Error when migrating TpTimings ", err.Error())
	}
}

func testTpTimITCheckData(t *testing.T) {
	result, err := tpTimMigrator.storDBOut.StorDB().GetTPTimings(
		tpTimings[0].TPid, tpTimings[0].ID)
	if err != nil {
		t.Error("Error when getting TpTimings ", err.Error())
	}
	if !reflect.DeepEqual(tpTimings[0], result[0]) {
		t.Errorf("Expecting: %+v, received: %+v", tpTimings[0], result[0])
	}
	result, err = tpTimMigrator.storDBIn.StorDB().GetTPTimings(
		tpTimings[0].TPid, tpTimings[0].ID)
	if err != utils.ErrNotFound {
		t.Error(err)
	}
}
