package request

import (
	"encoding/json"
	"jaecoo-be/app/database/entity"
	"jaecoo-be/utils/paginator"
)

type SalesAgentsQueryRequest struct {
	Name       *string               `json:"name"`
	JobTitle   *string               `json:"job_title"`
	AgentType  *string               `json:"agent_type"`
	Pagination *paginator.Pagination `json:"pagination"`
}

type SalesAgentsQueryRequestContext struct {
	Name      string `json:"name"`
	JobTitle  string `json:"job_title"`
	AgentType string `json:"agent_type"`
}

func (req SalesAgentsQueryRequestContext) ToParamRequest() SalesAgentsQueryRequest {
	var request SalesAgentsQueryRequest

	if name := req.Name; name != "" {
		request.Name = &name
	}
	if jobTitle := req.JobTitle; jobTitle != "" {
		request.JobTitle = &jobTitle
	}
	if agentType := req.AgentType; agentType != "" {
		request.AgentType = &agentType
	}

	return request
}

type SalesAgentsCreateRequest struct {
	Name              string   `json:"name" validate:"required"`
	JobTitle           *string  `json:"job_title"`
	Phone             *string  `json:"phone"`
	AgentType         []string `json:"agent_type"`
	ProfilePicturePath *string  `json:"profile_picture_path"`
}

func (req SalesAgentsCreateRequest) ToEntity() *entity.SalesAgents {
	agentTypeJSON, _ := json.Marshal(req.AgentType)
	agentTypeStr := string(agentTypeJSON)
	if agentTypeStr == "null" {
		agentTypeStr = ""
	}

	return &entity.SalesAgents{
		Name:              req.Name,
		JobTitle:           req.JobTitle,
		Phone:             req.Phone,
		AgentType:         &agentTypeStr,
		ProfilePicturePath: req.ProfilePicturePath,
	}
}

type SalesAgentsUpdateRequest struct {
	Name              *string  `json:"name"`
	JobTitle           *string  `json:"job_title"`
	Phone             *string  `json:"phone"`
	AgentType         []string `json:"agent_type"`
	ProfilePicturePath *string  `json:"profile_picture_path"`
	IsActive          *bool    `json:"is_active"`
}

func (req SalesAgentsUpdateRequest) ToEntity() *entity.SalesAgents {
	agentTypeJSON, _ := json.Marshal(req.AgentType)
	agentTypeStr := string(agentTypeJSON)
	if agentTypeStr == "null" {
		agentTypeStr = ""
	}

	return &entity.SalesAgents{
		Name:              getStringValue(req.Name),
		JobTitle:           req.JobTitle,
		Phone:             req.Phone,
		AgentType:         &agentTypeStr,
		ProfilePicturePath: req.ProfilePicturePath,
		IsActive:          req.IsActive,
	}
}

func getStringValue(s *string) string {
	if s == nil {
		return ""
	}
	return *s
}

