package request

import (
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/utils/paginator"
	"strconv"
)

type EducationHistoryQueryRequest struct {
	UserID         uint                  `json:"userId" validate:"required"`
	SchoolName     *string               `json:"schoolName"`
	Major          *string               `json:"major"`
	EducationLevel *string               `json:"educationLevel"`
	GraduationYear *int                  `json:"graduationYear"`
	Pagination     *paginator.Pagination `json:"pagination"`
}

type EducationHistoryCreateRequest struct {
	SchoolName       string `json:"schoolName" validate:"required,min=2,max=255"`
	Major            string `json:"major" validate:"required,min=2,max=255"`
	EducationLevel   string `json:"educationLevel" validate:"required,min=2,max=100"`
	GraduationYear   int    `json:"graduationYear" validate:"required,min=1950,max=2030"`
	CertificateImage string `json:"certificateImage,omitempty"`
}

func (req EducationHistoryCreateRequest) ToEntity(userId uint) *entity.EducationHistory {
	certificateImage := &req.CertificateImage
	if req.CertificateImage == "" {
		certificateImage = nil
	}

	return &entity.EducationHistory{
		UserID:           userId,
		SchoolName:       req.SchoolName,
		Major:            req.Major,
		EducationLevel:   req.EducationLevel,
		GraduationYear:   req.GraduationYear,
		CertificateImage: certificateImage,
	}
}

type EducationHistoryUpdateRequest struct {
	SchoolName       string `json:"schoolName" validate:"required,min=2,max=255"`
	Major            string `json:"major" validate:"required,min=2,max=255"`
	EducationLevel   string `json:"educationLevel" validate:"required,min=2,max=100"`
	GraduationYear   int    `json:"graduationYear" validate:"required,min=1950,max=2030"`
	CertificateImage string `json:"certificateImage,omitempty"`
}

func (req EducationHistoryUpdateRequest) ToEntity(userId uint) *entity.EducationHistory {
	certificateImage := &req.CertificateImage
	if req.CertificateImage == "" {
		certificateImage = nil
	}

	return &entity.EducationHistory{
		UserID:           userId,
		SchoolName:       req.SchoolName,
		Major:            req.Major,
		EducationLevel:   req.EducationLevel,
		GraduationYear:   req.GraduationYear,
		CertificateImage: certificateImage,
	}
}

type EducationHistoryQueryRequestContext struct {
	UserID         string `json:"userId"`
	SchoolName     string `json:"schoolName"`
	Major          string `json:"major"`
	EducationLevel string `json:"educationLevel"`
	GraduationYear string `json:"graduationYear"`
}

func (req EducationHistoryQueryRequestContext) ToParamRequest() EducationHistoryQueryRequest {
	var request EducationHistoryQueryRequest

	if userId := req.UserID; userId != "" {
		userIdUint, err := strconv.ParseUint(userId, 10, 0)
		if err == nil {
			request.UserID = uint(userIdUint)
		}
	}
	if schoolName := req.SchoolName; schoolName != "" {
		request.SchoolName = &schoolName
	}
	if major := req.Major; major != "" {
		request.Major = &major
	}
	if educationLevel := req.EducationLevel; educationLevel != "" {
		request.EducationLevel = &educationLevel
	}
	if graduationYearStr := req.GraduationYear; graduationYearStr != "" {
		graduationYear, err := strconv.Atoi(graduationYearStr)
		if err == nil {
			request.GraduationYear = &graduationYear
		}
	}

	return request
}
