package division

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

var now = time.Now()

func CreateSimgosData(input e.CreateDto, event *pl.Event, dbx ...*gorm.DB) (*esimgos.MUnit, 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 uint16, event *pl.Event) (*esimgos.MUnit, error) {
	pl.SetLogInfo(event, simgosId, "started", "DBReadDetail")
	data := esimgos.MUnit{}

	var tx = dg.IS["simrs"]

	if err := tx.
		Where("\"kode_unit\" = ?", 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.UpdateDto, dataSimgos *esync.DivisionLink, event *pl.Event, dbx ...*gorm.DB) error {
	pl.SetLogInfo(event, input, "started", "DBUpdate")

	data := setDataSimgos(&input)
	data.KodeUnit = dataSimgos.Simgos_Id

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

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