//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 v2

import (
	"path"
	"reflect"
	"testing"

	"github.com/cgrates/birpc/context"

	"github.com/cgrates/birpc"
	v1 "github.com/cgrates/cgrates/apier/v1"
	"github.com/cgrates/cgrates/config"
	"github.com/cgrates/cgrates/engine"
	"github.com/cgrates/cgrates/utils"
)

var (
	tpCfgPath string
	tpCfg     *config.CGRConfig
	tpRPC     *birpc.Client
	err       error
	delay     int
	configDIR string // relative path towards a config directory under samples prefix
	testTPid  = "V2TestTPit"

	// subtests to be executed for each confDIR
	sTestsTutIT = []func(t *testing.T){
		testTPitLoadConfig,
		testTPitResetDataDb,
		testTPitResetStorDb,
		testTPitStartEngine,
		testTPitRpcConn,
		testTPitTimings,
		testTPitDestinations,
		testTPitKillEngine,
	}
)

// Tests starting here

func TestTPit(t *testing.T) {

	switch *utils.DBType {
	case utils.MetaInternal:
		configDIR = "tutinternal"
	case utils.MetaMySQL:
		configDIR = "tutmysql"
	case utils.MetaMongo:
		configDIR = "tutmongo"
	case utils.MetaPostgres:
		configDIR = "tutpostgres"
	default:
		t.Fatal("Unknown Database type")
	}

	for _, stest := range sTestsTutIT {
		t.Run(configDIR, stest)
	}
}

func testTPitLoadConfig(t *testing.T) {
	tpCfgPath = path.Join(*utils.DataDir, "conf", "samples", configDIR)
	if tpCfg, err = config.NewCGRConfigFromPath(tpCfgPath); err != nil {
		t.Error(err)
	}
	switch configDIR {
	case "tutmongo": // Mongo needs more time to reset db, need to investigate
		delay = 4000
	default:
		delay = *utils.WaitRater
	}
}

// Remove data in both rating and accounting db
func testTPitResetDataDb(t *testing.T) {
	if err := engine.InitDataDB(tpCfg); err != nil {
		t.Fatal(err)
	}
}

// Wipe out the cdr database
func testTPitResetStorDb(t *testing.T) {
	if err := engine.InitStorDb(tpCfg); err != nil {
		t.Fatal(err)
	}
}

// Start CGR Engine
func testTPitStartEngine(t *testing.T) {
	if _, err := engine.StopStartEngine(tpCfgPath, delay); err != nil { // Mongo requires more time to start
		t.Fatal(err)
	}
}

// Connect rpc client to rater
func testTPitRpcConn(t *testing.T) {
	var err error
	tpRPC, err = newRPCClient(tpCfg.ListenCfg()) // We connect over JSON so we can also troubleshoot if needed
	if err != nil {
		t.Fatal(err)
	}
}

func testTPitTimings(t *testing.T) {
	// PEAK,*any,*any,*any,1;2;3;4;5,08:00:00
	tmPeak := &utils.ApierTPTiming{
		TPid:      testTPid,
		ID:        "PEAK",
		Years:     "*any",
		Months:    "*any",
		MonthDays: "*any",
		WeekDays:  "1;2;3;4;5",
		Time:      "08:00:00",
	}
	// OFFPEAK_MORNING,*any,*any,*any,1;2;3;4;5,00:00:00
	tmOffPeakMorning := &utils.ApierTPTiming{
		TPid:      testTPid,
		ID:        "OFFPEAK_MORNING",
		Years:     "*any",
		Months:    "*any",
		MonthDays: "*any",
		WeekDays:  "1;2;3;4;5",
		Time:      "00:00:00",
	}
	// OFFPEAK_EVENING,*any,*any,*any,1;2;3;4;5,19:00:00
	tmOffPeakEvening := &utils.ApierTPTiming{
		TPid:      testTPid,
		ID:        "OFFPEAK_EVENING",
		Years:     "*any",
		Months:    "*any",
		MonthDays: "*any",
		WeekDays:  "1;2;3;4;5",
		Time:      "19:00:00",
	}
	// OFFPEAK_WEEKEND,*any,*any,*any,6;7,00:00:00
	tmOffPeakWeekend := &utils.ApierTPTiming{
		TPid:      testTPid,
		ID:        "OFFPEAK_WEEKEND",
		Years:     "*any",
		Months:    "*any",
		MonthDays: "*any",
		WeekDays:  "6;7",
		Time:      "00:00:00",
	}
	// DUMMY, only used for the purpose of testing remove function
	tmDummyRemove := &utils.ApierTPTiming{
		TPid:      testTPid,
		ID:        "DUMMY_REMOVE",
		Years:     "*any",
		Months:    "*any",
		MonthDays: "*any",
		WeekDays:  "*any",
		Time:      "01:00:00",
	}
	// Test set
	var reply string
	for _, tm := range []*utils.ApierTPTiming{tmPeak, tmOffPeakMorning, tmOffPeakEvening, tmOffPeakWeekend, tmDummyRemove} {
		if err := tpRPC.Call(context.Background(), utils.APIerSv2SetTPTiming, tm, &reply); err != nil {
			t.Error("Got error on APIerSv2.SetTPTiming: ", err.Error())
		} else if reply != utils.OK {
			t.Error("Unexpected reply received when calling APIerSv2.SetTPTiming: ", reply)
		}
	}
	// Test get
	var rplyTmDummy *utils.ApierTPTiming
	if err := tpRPC.Call(context.Background(), utils.APIerSv2GetTPTiming, v1.AttrGetTPTiming{TPid: tmDummyRemove.TPid, ID: tmDummyRemove.ID}, &rplyTmDummy); err != nil {
		t.Error("Calling APIerSv2.GetTPTiming, got error: ", err.Error())
	} else if !reflect.DeepEqual(tmDummyRemove, rplyTmDummy) {
		t.Errorf("Calling APIerSv2.GetTPTiming expected: %v, received: %v", tmDummyRemove, rplyTmDummy)
	}
	var rplyTmIDs []string
	expectedTmIDs := []string{"OFFPEAK_EVENING", "OFFPEAK_MORNING", "OFFPEAK_WEEKEND", "PEAK", tmDummyRemove.ID}
	if err := tpRPC.Call(context.Background(), utils.APIerSv1GetTPTimingIds, &v1.AttrGetTPTimingIds{TPid: testTPid, PaginatorWithSearch: utils.PaginatorWithSearch{}}, &rplyTmIDs); err != nil {
		t.Error("Calling APIerSv1.GetTPTimingIds, got error: ", err.Error())
	} else if len(expectedTmIDs) != len(rplyTmIDs) {
		t.Errorf("Calling APIerSv1.GetTPTimingIds expected: %v, received: %v", expectedTmIDs, rplyTmIDs)
	}
	// Test remove
	if err := tpRPC.Call(context.Background(), utils.APIerSv2RemoveTPTiming, v1.AttrGetTPTiming{TPid: tmDummyRemove.TPid, ID: tmDummyRemove.ID}, &reply); err != nil {
		t.Error("Calling APIerSv2.RemoveTPTiming, got error: ", err.Error())
	} else if reply != utils.OK {
		t.Error("Calling APIerSv2.RemoveTPTiming received: ", reply)
	}
	// Test getIds
	rplyTmIDs = []string{}
	expectedTmIDs = []string{"OFFPEAK_EVENING", "OFFPEAK_MORNING", "OFFPEAK_WEEKEND", "PEAK"}
	if err := tpRPC.Call(context.Background(), utils.APIerSv1GetTPTimingIds, &v1.AttrGetTPTimingIds{TPid: testTPid, PaginatorWithSearch: utils.PaginatorWithSearch{}}, &rplyTmIDs); err != nil {
		t.Error("Calling APIerSv1.GetTPTimingIds, got error: ", err.Error())
	} else if len(expectedTmIDs) != len(rplyTmIDs) {
		t.Errorf("Calling APIerSv1.GetTPTimingIds expected: %v, received: %v", expectedTmIDs, rplyTmIDs)
	}
}

func testTPitDestinations(t *testing.T) {
	var reply string
	// DST_1002,1002
	dst1002 := &utils.TPDestination{TPid: testTPid, ID: "DST_1002", Prefixes: []string{"1002"}}
	// DST_1003,1003
	dst1003 := &utils.TPDestination{TPid: testTPid, ID: "DST_1003", Prefixes: []string{"1003"}}
	// DST_1007,1007
	dst1007 := &utils.TPDestination{TPid: testTPid, ID: "DST_1007", Prefixes: []string{"1007"}}
	// DST_FS,10
	dstFS := &utils.TPDestination{TPid: testTPid, ID: "DST_FS", Prefixes: []string{"10"}}
	// DST_DE_MOBILE,+49151
	// DST_DE_MOBILE,+49161
	// DST_DE_MOBILE,+49171
	dstDEMobile := &utils.TPDestination{TPid: testTPid, ID: "DST_DE_MOBILE", Prefixes: []string{"+49151", "+49161", "+49171"}}
	dstDUMMY := &utils.TPDestination{TPid: testTPid, ID: "DUMMY_REMOVE", Prefixes: []string{"999"}}
	for _, dst := range []*utils.TPDestination{dst1002, dst1003, dst1007, dstFS, dstDEMobile, dstDUMMY} {
		if err := tpRPC.Call(context.Background(), utils.APIerSv2SetTPDestination, dst, &reply); err != nil {
			t.Error("Got error on APIerSv2.SetTPDestination: ", err.Error())
		} else if reply != utils.OK {
			t.Error("Unexpected reply received when calling APIerSv2.SetTPDestination: ", reply)
		}
	}
	// Test get
	var rplyDst *utils.TPDestination
	if err := tpRPC.Call(context.Background(), utils.APIerSv2GetTPDestination, &AttrGetTPDestination{testTPid, dstDEMobile.ID}, &rplyDst); err != nil {
		t.Error("Calling APIerSv2.GetTPDestination, got error: ", err.Error())
	} else if len(dstDEMobile.Prefixes) != len(rplyDst.Prefixes) {
		t.Errorf("Calling APIerSv2.GetTPDestination expected: %v, received: %v", dstDEMobile, rplyDst)
	}
	// Test remove
	if err := tpRPC.Call(context.Background(), utils.APIerSv2RemoveTPDestination, &AttrGetTPDestination{testTPid, dstDUMMY.ID}, &reply); err != nil {
		t.Error(err)
	} else if reply != utils.OK {
		t.Error("Received: ", reply)
	}
	// Test getIds
	var rplyDstIds []string
	expectedDstIds := []string{"DST_1002", "DST_1003", "DST_1007", "DST_DE_MOBILE", "DST_FS"}
	if err := tpRPC.Call(context.Background(), utils.APIerSv2GetTPDestinationIDs, v1.AttrGetTPDestinationIds{TPid: testTPid}, &rplyDstIds); err != nil {
		t.Error("Calling APIerSv1.GetTPDestinationIDs, got error: ", err.Error())
	} else if len(expectedDstIds) != len(rplyDstIds) {
		t.Errorf("Calling APIerSv2.GetTPDestinationIDs expected: %v, received: %v", expectedDstIds, rplyDstIds)
	}
}

func testTPitKillEngine(t *testing.T) {
	if err := engine.KillEngine(100); err != nil {
		t.Error(err)
	}
}
