package request

import (
	"netidhub-saas-be/utils/paginator"
	"strconv"
	"time"
)

type ArticleApprovalFlowsGeneric interface {
	ToEntity()
}

type ArticleApprovalFlowsQueryRequest struct {
	ArticleId   *int                  `json:"articleId"`
	WorkflowId  *int                  `json:"workflowId"`
	StatusId    *int                  `json:"statusId"`
	SubmittedBy *int                  `json:"submittedBy"`
	CurrentStep *int                  `json:"currentStep"`
	DateFrom    *time.Time            `json:"dateFrom"`
	DateTo      *time.Time            `json:"dateTo"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type SubmitForApprovalRequest struct {
	ArticleId  int  `json:"articleId" validate:"required"`
	WorkflowId *int `json:"workflowId"`
}

type ApprovalActionRequest struct {
	Action  string `json:"action" validate:"required,oneof=approve revision reject"`
	Message string `json:"message"`
}

type RejectionRequest struct {
	Reason string `json:"reason" validate:"required"`
}

type RevisionRequest struct {
	Message string `json:"message" validate:"required"`
}

type ResubmitRequest struct {
	Message string `json:"message"`
}

type ArticleApprovalFlowsQueryRequestContext struct {
	ArticleId   string `json:"articleId"`
	WorkflowId  string `json:"workflowId"`
	StatusId    string `json:"statusId"`
	SubmittedBy string `json:"submittedBy"`
	CurrentStep string `json:"currentStep"`
	DateFrom    string `json:"dateFrom"`
	DateTo      string `json:"dateTo"`
}

func (req ArticleApprovalFlowsQueryRequestContext) ToParamRequest() ArticleApprovalFlowsQueryRequest {
	var articleId *int
	var workflowId *int
	var statusId *int
	var submittedBy *int
	var currentStep *int
	var dateFrom *time.Time
	var dateTo *time.Time

	if req.ArticleId != "" {
		if parsedArticleId, err := strconv.Atoi(req.ArticleId); err == nil {
			articleId = &parsedArticleId
		}
	}

	if req.WorkflowId != "" {
		if parsedWorkflowId, err := strconv.Atoi(req.WorkflowId); err == nil {
			workflowId = &parsedWorkflowId
		}
	}

	if req.StatusId != "" {
		if parsedStatusId, err := strconv.Atoi(req.StatusId); err == nil {
			statusId = &parsedStatusId
		}
	}

	if req.SubmittedBy != "" {
		if parsedSubmittedBy, err := strconv.Atoi(req.SubmittedBy); err == nil {
			submittedBy = &parsedSubmittedBy
		}
	}

	if req.CurrentStep != "" {
		if parsedCurrentStep, err := strconv.Atoi(req.CurrentStep); err == nil {
			currentStep = &parsedCurrentStep
		}
	}

	if req.DateFrom != "" {
		if parsedDateFrom, err := time.Parse("2006-01-02", req.DateFrom); err == nil {
			dateFrom = &parsedDateFrom
		}
	}

	if req.DateTo != "" {
		if parsedDateTo, err := time.Parse("2006-01-02", req.DateTo); err == nil {
			dateTo = &parsedDateTo
		}
	}

	return ArticleApprovalFlowsQueryRequest{
		ArticleId:   articleId,
		WorkflowId:  workflowId,
		StatusId:    statusId,
		SubmittedBy: submittedBy,
		CurrentStep: currentStep,
		DateFrom:    dateFrom,
		DateTo:      dateTo,
	}
}
