package server_grpc

import (
	"context"
	"github.com/ManyakRus/starter/config_main"
	"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/crud_starter"
	"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/network/grpc/grpc_proto"
	"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types"
	"testing"
)

// LawsuitStatusType_ID_Test - ID таблицы для тестирования
const LawsuitStatusType_ID_Test = 1

func Test_server_LawsuitStatusType_Read(t *testing.T) {
	config_main.LoadEnv()
	crud_starter.InitCrudTransport_DB()

	ctx := context.Background()
	Request := grpc_proto.RequestId{}
	Request.ID = LawsuitStatusType_ID_Test
	Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()

	server1 := &ServerGRPC{}
	Otvet, err := server1.LawsuitStatusType_Read(ctx, &Request)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_Read() error: ", err)
	}
	if Otvet.ModelString == "" {
		t.Error("Test_server_LawsuitStatusType_Read() error: ModelString=''")
	}
}

func Test_server_LawsuitStatusType_Delete(t *testing.T) {
	config_main.LoadEnv()
	crud_starter.InitCrudTransport_DB()

	ctx := context.Background()
	Request := grpc_proto.RequestId{}
	Request.ID = LawsuitStatusType_ID_Test
	Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()

	server1 := &ServerGRPC{}
	Otvet, err := server1.LawsuitStatusType_Delete(ctx, &Request)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_Delete() error: ", err)
	}
	if Otvet.ModelString == "" {
		t.Error("Test_server_LawsuitStatusType_Delete() error: ModelString=''")
	}
	Otvet, err = server1.LawsuitStatusType_Restore(ctx, &Request)
}

func Test_server_LawsuitStatusType_Create(t *testing.T) {
	config_main.LoadEnv()
	crud_starter.InitCrudTransport_DB()

	var ModelString string
	Model := lawsuit_status_types.LawsuitStatusType{}
	Model.ID = 1
	ModelString, err := Model.GetJSON()
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_Create() error: ", err)
		return
	}

	RequestModel := grpc_proto.RequestModel{}
	RequestModel.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()
	RequestModel.ModelString = ModelString

	ctx := context.Background()
	server1 := &ServerGRPC{}
	Otvet, err := server1.LawsuitStatusType_Create(ctx, &RequestModel)
	if err == nil {
		t.Error("Test_server_LawsuitStatusType_Create() error: ", err)
	}
	if Otvet.ModelString != "" {
		t.Error("Test_server_LawsuitStatusType_Create() error: ModelString !=''")
	}
}

func Test_server_LawsuitStatusType_Update(t *testing.T) {
	config_main.LoadEnv()
	crud_starter.InitCrudTransport_DB()

	ctx := context.Background()
	Request := grpc_proto.RequestId{}
	Request.ID = LawsuitStatusType_ID_Test
	Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()

	server1 := &ServerGRPC{}
	Response1, err := server1.LawsuitStatusType_Read(ctx, &Request)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_Update() error: ", err)
		return
	}
	if Response1.ModelString == "" {
		t.Error("Test_server_LawsuitStatusType_Update() error: ModelString=''")
	}

	var ModelString string
	ModelString = Response1.ModelString

	RequestModel := grpc_proto.RequestModel{}
	RequestModel.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()
	RequestModel.ModelString = ModelString

	Otvet, err := server1.LawsuitStatusType_Update(ctx, &RequestModel)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_Update() error: ", err)
	}
	if Otvet.ModelString == "" {
		t.Error("Test_server_LawsuitStatusType_Update() error: ModelString=''")
	}

}

func Test_server_LawsuitStatusType_Save(t *testing.T) {
	config_main.LoadEnv()
	crud_starter.InitCrudTransport_DB()

	ctx := context.Background()
	Request := grpc_proto.RequestId{}
	Request.ID = LawsuitStatusType_ID_Test
	Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()

	server1 := &ServerGRPC{}
	Response1, err := server1.LawsuitStatusType_Read(ctx, &Request)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_Save() error: ", err)
		return
	}

	var ModelString string
	ModelString = Response1.ModelString

	// sModel, _ := GetJSON(Otvet)
	RequestModel := grpc_proto.RequestModel{}
	RequestModel.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()
	RequestModel.ModelString = ModelString

	Otvet, err := server1.LawsuitStatusType_Save(ctx, &RequestModel)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_Save() error: ", err)
	}
	if Otvet.ModelString == "" {
		t.Error("Test_server_LawsuitStatusType_Save() error: ModelString=''")
	}

}

func Test_server_LawsuitStatusType_FindByExtID(t *testing.T) {
	config_main.LoadEnv()
	crud_starter.InitCrudTransport_DB()

	ctx := context.Background()
	Request := grpc_proto.RequestId{}
	Request.ID = LawsuitStatusType_ID_Test
	Request.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()

	server1 := &ServerGRPC{}
	Otvet1, err := server1.LawsuitStatusType_Read(ctx, &Request)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_FindByExtID() error: ", err)
	}

	// получим модель из строки JSON
	Model := lawsuit_status_types.LawsuitStatusType{}
	err = Model.GetModelFromJSON(Otvet1.ModelString)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_FindByExtID() GetModelFromJSON() error: ", err)
	}

	if Model.ExtID == 0 {
		return
	}


	ctx = context.Background()
	RequestExtID:= grpc_proto.RequestExtID{}
	RequestExtID.ExtID = Model.ExtID
	RequestExtID.ConnectionId = Model.ConnectionID
	RequestExtID.VersionModel = lawsuit_status_types.LawsuitStatusType{}.GetStructVersion()

	Otvet, err := server1.LawsuitStatusType_FindByExtID(ctx, &RequestExtID)
	if err != nil {
		t.Error("Test_server_LawsuitStatusType_FindByExtID() error: ", err)
	}
	if Otvet.ModelString == "" {
		t.Error("Test_server_LawsuitStatusType_FindByExtID() error: ModelString=''")
	}
}
