package internal_reference

import (
	plh "simrs-vx/pkg/lib-helper"
	pl "simrs-vx/pkg/logger"
	pu "simrs-vx/pkg/use-case-helper"
	"time"

	dg "github.com/karincake/apem/db-gorm-pg"
	"gorm.io/gorm"

	e "simrs-vx/internal/domain/main-entities/encounter"
	eir "simrs-vx/internal/domain/main-entities/internal-reference"

	etph "simrs-vx/internal/domain/simgos-entities/t-pemeriksaan-hist"
	etp "simrs-vx/internal/domain/simgos-entities/t-pendaftaran"

	esync "simrs-vx/internal/domain/sync-entities/internal-reference"
	esynclog "simrs-vx/internal/domain/sync-entities/log"
)

const source = "internal-reference"

var now = time.Now()

func CreateSimgosData(input e.Encounter, dataDaftar *etp.TPendaftaran, event *pl.Event, dbx ...*gorm.DB) (*[]etph.TPemeriksaanHist, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")

	data := setCreateDataSimgos(input, dataDaftar)

	var tx *gorm.DB
	if len(dbx) > 0 {
		tx = dbx[0]
	} else {
		tx = dg.IS["simrs"]
	}

	if err := tx.Create(&data).Error; err != nil {
		return nil, plh.HandleCreateError(input, event, err)
	}

	pl.SetLogInfo(event, nil, "complete")
	return &data, nil
}

func ReadDetailSimgosData(simgosId uint, event *pl.Event) (*etph.TPemeriksaanHist, error) {
	pl.SetLogInfo(event, simgosId, "started", "DBReadDetail")
	data := etph.TPemeriksaanHist{}

	var tx = dg.IS["simrs"]

	if err := tx.
		Where("\"id_pemeriksaanhist\" = ?", simgosId).
		First(&data).Error; err != nil {
		if processedErr := pu.HandleReadError(err, event, source, simgosId, data); processedErr != nil {
			return nil, processedErr
		}
	}

	pl.SetLogInfo(event, nil, "complete")
	return &data, nil
}

func UpdateSimgosData(input e.ApproveCancelUnitDto, data *etph.TPemeriksaanHist, method string, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, input, "started", "DBUpdate")

	switch method {
	case "approve":
		setUpdateApproveDataSimgos(input, data)
	case "cancel":
		setUpdateCancelDataSimgos(input, data)
	}

	var tx *gorm.DB
	if len(dbx) > 0 {
		tx = dbx[0]
	} else {
		tx = dg.IS["simrs"]
	}

	if err := tx.Save(&data).Error; err != nil {
		event.Status = "failed"
		event.ErrInfo = pl.ErrorInfo{
			Code:   "data-update-fail",
			Detail: "Database update failed",
			Raw:    err,
		}
		return pl.SetLogError(event, input)
	}

	pl.SetLogInfo(event, nil, "complete")
	return nil
}

func CreateBulkLinkData(ir []eir.InternalReference, tph []etph.TPemeriksaanHist, event *pl.Event, dbx ...*gorm.DB) (*[]esync.InternalReferenceLink, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")
	data := setDataSimxLink(ir, tph)

	var tx *gorm.DB
	if len(dbx) > 0 {
		tx = dbx[0]
	} else {
		tx = dg.I
	}

	if err := tx.Create(&data).Error; err != nil {
		return nil, plh.HandleCreateError(data, event, err)
	}

	pl.SetLogInfo(event, nil, "complete")
	return &data, nil
}

func ReadDetailLinkData(simxId uint, event *pl.Event) (*esync.InternalReferenceLink, error) {
	pl.SetLogInfo(event, simxId, "started", "DBReadDetail")
	data := esync.InternalReferenceLink{}

	var tx = dg.I

	if err := tx.
		Where("\"Simx_Id\" = ?", simxId).
		First(&data).Error; err != nil {
		if processedErr := pu.HandleReadError(err, event, source, simxId, data); processedErr != nil {
			return nil, processedErr
		}
	}

	pl.SetLogInfo(event, nil, "complete")
	return &data, nil
}

func DeleteLinkData(data *[]esync.InternalReferenceLink, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, data, "started", "DBDelete")

	var tx *gorm.DB
	if len(dbx) > 0 {
		tx = dbx[0]
	} else {
		tx = dg.I
	}

	if err := tx.Delete(*data).Error; err != nil {
		event.Status = "failed"
		event.ErrInfo = pl.ErrorInfo{
			Code:   "data-delete-fail",
			Detail: "Database delete failed",
			Raw:    err,
		}
		return pl.SetLogError(event, data)
	}

	pl.SetLogInfo(event, nil, "complete")
	return nil
}

func CreateLogData(input esynclog.SimxLogDto, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, nil, "started", "DBCreate")
	data := setDataSimxLog(&input)

	var tx *gorm.DB
	if len(dbx) > 0 {
		tx = dbx[0]
	} else {
		tx = dg.I
	}

	if err := tx.Create(&data).Error; err != nil {
		return plh.HandleCreateError(input, event, err)
	}

	pl.SetLogInfo(event, nil, "complete")
	return nil
}
