package encounter

import (
	etph "simrs-vx/internal/domain/simgos-entities/t-pemeriksaan-hist"
	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"
	etp "simrs-vx/internal/domain/simgos-entities/t-pendaftaran"
	esync "simrs-vx/internal/domain/sync-entities/encounter"
	esynclog "simrs-vx/internal/domain/sync-entities/log"
)

var now = time.Now()

func CreateTPendaftaranData(input e.Encounter, event *pl.Event, dbx ...*gorm.DB) (*etp.TPendaftaran, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")

	data, err := setDataCreateTPendaftaran(&input)
	if err != nil {
		return nil, plh.HandleCreateError(input, event, err)
	}

	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 ReadTPendaftaranDetailData(simgosId uint, event *pl.Event) (*etp.TPendaftaran, error) {
	pl.SetLogInfo(event, simgosId, "started", "DBReadDetail")
	data := etp.TPendaftaran{}

	var tx = dg.IS["simrs"]

	if err := tx.
		Where("\"idxdaftar\" = ?", 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 UpdateTPendaftaranData(input e.Encounter, data *etp.TPendaftaran, method string, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, input, "started", "DBUpdate")

	switch method {
	case "update":
		setDataTPendaftaran(&input, data)
	case "check-in":
		setDataCheckIn(input, data)
	case "check-out":
		setDataCheckOut(input, data)
	case "update-status":
		setDataUpdateStatus(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 HardDeleteTPendaftaranData(data *etp.TPendaftaran, 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.IS["simrs"]
	}

	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 CreateLinkData(simxId, simgosId uint, event *pl.Event, dbx ...*gorm.DB) (*esync.EncounterLink, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")
	data := setDataSimxLink(simxId, simgosId)

	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 CreateBulkLinkData(simxId, simgosId uint, event *pl.Event, dbx ...*gorm.DB) (*esync.EncounterLink, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")
	data := setDataSimxLink(simxId, simgosId)

	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.EncounterLink, error) {
	pl.SetLogInfo(event, simxId, "started", "DBReadDetail")
	data := esync.EncounterLink{}

	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.EncounterLink, 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
}

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

	hist = setDataRequestSwitchUnit(input, data)

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

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

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

func UpdateTPemeriksaanHistData(input e.ApproveCancelUnitDto, data *etph.TPemeriksaanHist, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, input, "started", "DBUpdateTPemeriksaanHist")

	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
}
