package division

import (
	pl "simrs-vx/pkg/logger"

	d "github.com/karincake/dodol"
	"gorm.io/gorm"

	db "simrs-vx/pkg/dualtrx-helper"

	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"
	elog "simrs-vx/internal/domain/sync-entities/log"
)

const source = "division"

func Create(input e.CreateDto) (*d.Data, error) {
	var (
		sgData   *esimgos.MUnit
		syncLink *esync.DivisionLink
		err      error
	)

	event := pl.Event{
		Feature: "Create",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "create")

	err = db.WithDualTx(func(tx *db.Dualtx) error {
		// STEP 1: Insert to simgos
		sgData, err = CreateSimgosData(input, &event, tx.Simgos)
		if err != nil {
			return err
		}

		// STEP 2: Insert to Link
		syncLink, err = CreateLinkData(*input.Id, sgData.KodeUnit, &event, tx.Sync)
		if err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		if syncLink != nil {
			go func() { _ = DeleteLinkData(syncLink, &event) }()
		}
		return nil, err
	}

	pl.SetLogInfo(&event, nil, "complete")

	return &d.Data{
		Meta: d.II{
			"source":    source,
			"structure": "single-data",
			"status":    "created",
		},
	}, nil
}

func CreateSimxLog(input elog.SimxLogDto) (*d.Data, error) {
	event := pl.Event{
		Feature: "Create",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "create")

	tx := db.NewTx()
	err := tx.Sync.Transaction(func(tx *gorm.DB) error {
		// Insert to Log
		if err := CreateLogData(input, &event, tx); err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	pl.SetLogInfo(&event, nil, "complete")

	return &d.Data{
		Meta: d.II{
			"source":    source,
			"structure": "single-data",
			"status":    "created",
		},
	}, nil
}

func Update(input e.UpdateDto) (*d.Data, error) {
	event := pl.Event{
		Feature: "Update",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "update")

	// STEP 1: Get Link
	syncLink, err := ReadDetailLinkData(*input.Id, &event)
	if err != nil {
		return nil, err
	}

	tx := db.NewTx()
	err = tx.Simgos.Transaction(func(tx *gorm.DB) error {
		// Step 2: Update Simgos
		if err = UpdateSimgosData(input, syncLink, &event, tx); err != nil {
			return err
		}

		return nil
	})

	pl.SetLogInfo(&event, nil, "complete")

	return &d.Data{
		Meta: d.IS{
			"source":    source,
			"structure": "single-data",
			"status":    "updated",
		},
	}, nil
}

func Delete(input e.DeleteDto) (*d.Data, error) {
	var isLinkDeleted bool

	event := pl.Event{
		Feature: "Delete",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "delete")

	// STEP 1: Get Link
	syncLink, err := ReadDetailLinkData(*input.Id, &event)
	if err != nil {
		return nil, err
	}

	// STEP 2: Get Simgos
	sgData, err := ReadDetailSimgosData(uint16(syncLink.Simgos_Id), &event)
	if err != nil {
		return nil, err
	}

	err = db.WithDualTx(func(tx *db.Dualtx) error {
		// STEP 3: Delete Simgos
		err = HardDeleteSimgosData(sgData, &event, tx.Simgos)
		if err != nil {
			return err
		}

		// STEP 4: Delete Link
		err = DeleteLinkData(syncLink, &event, tx.Sync)
		if err != nil {
			return err
		}

		isLinkDeleted = true
		return nil
	})

	if err != nil {
		if isLinkDeleted {
			go func() {
				_, _ = CreateLinkData(uint(*input.Id), sgData.KodeUnit, &event)
			}()
		}
		return nil, err
	}

	pl.SetLogInfo(&event, nil, "complete")

	return &d.Data{
		Meta: d.IS{
			"source":    source,
			"structure": "single-data",
			"status":    "deleted",
		},
	}, nil

}
