/*
DESCRIPTION:
Any functions that are used internally by the use-case
*/
package user

import (
	"errors"

	"gorm.io/gorm"

	pl "simrs-vx/pkg/logger"
	p "simrs-vx/pkg/password"

	ue "simrs-vx/internal/use-case/main-use-case/employee"
	usi "simrs-vx/internal/use-case/main-use-case/specialist-intern"

	ee "simrs-vx/internal/domain/main-entities/employee"
	esi "simrs-vx/internal/domain/main-entities/specialist-intern"
	e "simrs-vx/internal/domain/main-entities/user"
	erc "simrs-vx/internal/domain/references/common"
	erg "simrs-vx/internal/domain/references/organization"
)

func setCreate(src e.CreateDto, dst *e.User) error {
	pass, err := p.Hash(src.Password)
	if err != nil {
		return err
	}

	dst.Name = src.Name
	dst.Password = pass
	dst.Status_Code = erc.USCActive
	dst.ContractPosition_Code = src.ContractPosition_Code

	return nil
}

func setUpdate(src e.UpdateDto, dst *e.User) {
	dst.Status_Code = src.Status_Code
	dst.ContractPosition_Code = src.ContractPosition_Code
}

func setDataEmployeeUpdate(src e.EmployeUpdateDto) ee.UpdateDto {
	return ee.UpdateDto{
		Id: src.Id,
		CreateDto: ee.CreateDto{
			User_Id:       src.User_Id,
			Person_Id:     src.Person_Id,
			Number:        src.Number,
			Status_Code:   src.Status_Code,
			Position_Code: &src.Position_Code,
		},
	}
}

func getPersonIdByUserId(userId uint, positionCode erg.ContractPositionCode, event *pl.Event, tx *gorm.DB) (*uint, error) {
	pl.SetLogInfo(event, nil, "started", "DBGetPersonIdByUserId")
	if positionCode == erg.CSCEmp {
	} else if positionCode == erg.CSCInt {
		specInt, err := usi.ReadDetailData(esi.ReadDetailDto{User_Id: &userId}, event, tx)
		if err != nil {
			return nil, err
		}
		if specInt.Person_Id == nil {
			return nil, errors.New("person id not found")
		}
		return specInt.Person_Id, nil
	}

	emp, err := ue.ReadDetailData(ee.ReadDetailDto{User_Id: &userId}, event, tx)
	if err != nil {
		return nil, err
	}

	return emp.Person_Id, nil
}
