package patient

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/patient"
	esimgos "simrs-vx/internal/domain/simgos-entities/m-pasien"
	esynclog "simrs-vx/internal/domain/sync-entities/log"
	esync "simrs-vx/internal/domain/sync-entities/patient"
)

var now = time.Now()

func CreateSimgosData(input e.Patient, event *pl.Event, dbx ...*gorm.DB) (*esimgos.MPasien, error) {
	pl.SetLogInfo(event, nil, "started", "DBCreate")

	data := setDataSimgos(&input)

	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) (*esimgos.MPasien, error) {
	pl.SetLogInfo(event, simgosId, "started", "DBReadDetail")
	data := esimgos.MPasien{}

	var tx = dg.IS["simrs"]

	if err := tx.
		Where("\"id\" = ?", 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 ReadDetailSimgosByNomrData(nomr string, event *pl.Event, dbx ...*gorm.DB) (*esimgos.MPasien, error) {
	pl.SetLogInfo(event, nomr, "started", "DBReadDetail")
	data := esimgos.MPasien{}

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

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

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

func UpdateSimgosData(input e.Patient, patientData *esimgos.MPasien, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, input, "started", "DBUpdate")

	data := setDataSimgos(&input)
	data.Id = patientData.Id
	data.CreatedAt = patientData.CreatedAt

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

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