package soapi

import (
	"errors"
	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/soapi"

	etdt "simrs-vx/internal/domain/simgos-entities/t-diagnosadanterapi"
	eicd "simrs-vx/internal/domain/simgos-entities/t-icd"
	eicdcm "simrs-vx/internal/domain/simgos-entities/t-icd-cm"
	ep "simrs-vx/internal/domain/simgos-entities/t-pendaftaran"

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

var now = time.Now()

func CreateTDiagnosaDanTerapi(input e.Soapi, tdaftar *ep.TPendaftaran, event *pl.Event, dbx ...*gorm.DB) (*etdt.TDiagnosaDanTerapi, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")
	data := setDataCreateTDiagnosaDanTerapi(input, tdaftar)

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

	var tx = dg.IS["simrs"]

	if err := tx.
		Where("\"idxterapi\" = ?", 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 UpdateTDiagnosaDanTerapi(input e.Soapi, data *etdt.TDiagnosaDanTerapi, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, input, "started", "DBUpdate")

	setEarlyMedicTDT(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 HardDeleteTDiagnosaDanTerapi(data *etdt.TDiagnosaDanTerapi, 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 CreateTIcd(input e.Soapi, tdaftar *ep.TPendaftaran, event *pl.Event, dbx ...*gorm.DB) (*[]eicd.TIcd, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")
	data := setDataIcd(input, tdaftar)

	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 ReadListTIcd(idxdaftar string, event *pl.Event, dbx ...*gorm.DB) ([]eicd.TIcd, error) {
	pl.SetLogInfo(event, idxdaftar, "started", "DBReadList")
	data := []eicd.TIcd{}

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

	tx = tx.
		Model(&eicd.TIcd{}).
		Where("idxdaftar = ?", idxdaftar)

	if err := tx.Find(&data).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, nil
		}
		return nil, plh.HandleListError(idxdaftar, event, err)
	}

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

func HardDeleteTIcd(idxdaftar uint, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, idxdaftar, "started", "DBDelete")

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

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

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

func CreateTIcdCm(input e.Soapi, tdaftar *ep.TPendaftaran, event *pl.Event, dbx ...*gorm.DB) (*[]eicdcm.TIcdCm, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")
	data := setDataIcdCm(input, tdaftar)

	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 ReadListTIcdCm(idxdaftar string, event *pl.Event, dbx ...*gorm.DB) ([]eicdcm.TIcdCm, error) {
	pl.SetLogInfo(event, idxdaftar, "started", "DBReadList")
	data := []eicdcm.TIcdCm{}

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

	tx = tx.
		Model(&eicdcm.TIcdCm{}).
		Where("idxdaftar = ?", idxdaftar)

	if err := tx.Find(&data).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, nil
		}
		return nil, plh.HandleListError(idxdaftar, event, err)
	}

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

func HardDeleteTIcdCm(idxdaftar uint, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, idxdaftar, "started", "DBDelete")

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

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

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

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

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