package request

import (
	"go-humas-be/app/database/entity"
	"go-humas-be/utils/paginator"
	"strconv"
	"time"
)

type UsersGeneric interface {
	ToEntity()
}

type UsersQueryRequest struct {
	Username       *string               `json:"username"`
	Email          *string               `json:"email"`
	Fullname       *string               `json:"fullname"`
	PhoneNumber    *string               `json:"phoneNumber"`
	IdentityNumber *string               `json:"identityNumber"`
	UserRoleId     *int                  `json:"userRoleId"`
	StatusId       *int                  `json:"statusId"`
	Pagination     *paginator.Pagination `json:"pagination"`
}

type UsersCreateRequest struct {
	Username       string `json:"username" validate:"required,lowercase"`
	Email          string `json:"email" validate:"required,email"`
	Fullname       string `json:"fullname" validate:"required"`
	Address        string `json:"address" validate:"required"`
	PhoneNumber    string `json:"phoneNumber" validate:"required"`
	WorkType       string `json:"workType" validate:"required"`
	GenderType     string `json:"genderType" validate:"required"`
	IdentityType   string `json:"identityType" validate:"required"`
	IdentityNumber string `json:"identityNumber" validate:"required"`
	DateOfBirth    string `json:"dateOfBirth" validate:"required"`
	LastEducation  string `json:"lastEducation" validate:"required"`
	UserRoleId     int    `json:"userRoleId" validate:"required"`
	UserLevelId    int    `json:"userLevelId" validate:"required"`
	Password       string `json:"password" validate:"required"`
}

func (req UsersCreateRequest) ToEntity() *entity.Users {
	return &entity.Users{
		Username:       req.Username,
		Email:          req.Email,
		Fullname:       req.Fullname,
		Address:        req.Address,
		PhoneNumber:    req.PhoneNumber,
		WorkType:       req.WorkType,
		GenderType:     req.GenderType,
		IdentityType:   req.IdentityType,
		IdentityNumber: req.IdentityNumber,
		DateOfBirth:    req.DateOfBirth,
		LastEducation:  req.LastEducation,
		UserRoleId:     req.UserRoleId,
		UserLevelId:    req.UserLevelId,
	}
}

type UsersUpdateRequest struct {
	Username       string `json:"username" validate:"required,lowercase"`
	Email          string `json:"email" validate:"required,email"`
	Fullname       string `json:"fullname" validate:"required"`
	Address        string `json:"address" validate:"required"`
	PhoneNumber    string `json:"phoneNumber" validate:"required"`
	WorkType       string `json:"workType" validate:"required"`
	GenderType     string `json:"genderType" validate:"required"`
	IdentityType   string `json:"identityType" validate:"required"`
	IdentityNumber string `json:"identityNumber" validate:"required"`
	DateOfBirth    string `json:"dateOfBirth" validate:"required"`
	LastEducation  string `json:"lastEducation" validate:"required"`
	UserRoleId     int    `json:"userRoleId" validate:"required"`
	UserLevelId    int    `json:"userLevelId" validate:"required"`
	StatusId       *int   `json:"statusId"`
}

func (req UsersUpdateRequest) ToEntity() *entity.Users {
	return &entity.Users{
		Username:       req.Username,
		Email:          req.Email,
		Fullname:       req.Fullname,
		Address:        req.Address,
		PhoneNumber:    req.PhoneNumber,
		WorkType:       req.WorkType,
		GenderType:     req.GenderType,
		IdentityType:   req.IdentityType,
		IdentityNumber: req.IdentityNumber,
		DateOfBirth:    req.DateOfBirth,
		LastEducation:  req.LastEducation,
		UserRoleId:     req.UserRoleId,
		StatusId:       req.StatusId,
		UserLevelId:    req.UserLevelId,
		UpdatedAt:      time.Now(),
	}
}

type UserLogin struct {
	Username string `json:"username" validate:"required,lowercase"`
	Password string `json:"password" validate:"required"`
}

type UsersQueryRequestContext struct {
	Username       string `json:"username"`
	Email          string `json:"email"`
	Fullname       string `json:"fullname"`
	Address        string `json:"address"`
	PhoneNumber    string `json:"phoneNumber"`
	IdentityNumber string `json:"identityNumber"`
	UserRoleId     string `json:"userRoleId"`
	StatusId       string `json:"statusId"`
}

func (req UsersQueryRequestContext) ToParamRequest() UsersQueryRequest {
	var request UsersQueryRequest

	if username := req.Username; username != "" {
		request.Username = &username
	}
	if email := req.Email; email != "" {
		request.Email = &email
	}
	if fullname := req.Fullname; fullname != "" {
		request.Fullname = &fullname
	}
	if phoneNumber := req.PhoneNumber; phoneNumber != "" {
		request.PhoneNumber = &phoneNumber
	}
	if identityNumber := req.IdentityNumber; identityNumber != "" {
		request.IdentityNumber = &identityNumber
	}
	if userRoleIdStr := req.UserRoleId; userRoleIdStr != "" {
		userRoleId, err := strconv.Atoi(userRoleIdStr)
		if err == nil {
			request.UserRoleId = &userRoleId
		}
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}

	return request
}
