package mapper

import (
	"netidhub-saas-be/app/database/entity"
	res "netidhub-saas-be/app/module/article_approval_step_logs/response"
	usersRepository "netidhub-saas-be/app/module/users/repository"

	"github.com/google/uuid"
	"github.com/rs/zerolog"
)

func ArticleApprovalStepLogsResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	articleApprovalStepLogsReq *entity.ArticleApprovalStepLogs,
	usersRepo usersRepository.UsersRepository,
) (articleApprovalStepLogsRes *res.ArticleApprovalStepLogsResponse) {

	if articleApprovalStepLogsReq != nil {
		// Map entity fields to response fields
		// approvalStatusID := uint(1) // Default status
		// if articleApprovalStepLogsReq.Action == "approved" {
		// 	approvalStatusID = 2
		// } else if articleApprovalStepLogsReq.Action == "rejected" {
		// 	approvalStatusID = 3
		// }

		articleApprovalStepLogsRes = &res.ArticleApprovalStepLogsResponse{
			ID:             articleApprovalStepLogsReq.ID,
			ApprovalFlowId: articleApprovalStepLogsReq.ApprovalFlowId,
			StepOrder:      articleApprovalStepLogsReq.StepOrder,
			StepName:       articleApprovalStepLogsReq.StepName,
			ApprovedById:   articleApprovalStepLogsReq.ApprovedById,
			Action:         articleApprovalStepLogsReq.Action,
			Message:        articleApprovalStepLogsReq.Message,
			ProcessedAt:    articleApprovalStepLogsReq.ProcessedAt,
			UserLevelId:    articleApprovalStepLogsReq.UserLevelId,
			ClientId: func() *string {
				if articleApprovalStepLogsReq.ClientId != nil {
					s := articleApprovalStepLogsReq.ClientId.String()
					return &s
				}
				return nil
			}(),
			CreatedAt: articleApprovalStepLogsReq.CreatedAt,
		}
	}

	return articleApprovalStepLogsRes
}

func ArticleApprovalStepLogsDetailResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	articleApprovalStepLogsReq *entity.ArticleApprovalStepLogs,
	usersRepo usersRepository.UsersRepository,
) (articleApprovalStepLogsRes *res.ArticleApprovalStepLogsDetailResponse) {

	if articleApprovalStepLogsReq != nil {
		// Map entity fields to response fields
		approvalStatusID := uint(1) // Default status
		if articleApprovalStepLogsReq.Action == "approved" {
			approvalStatusID = 2
		} else if articleApprovalStepLogsReq.Action == "rejected" {
			approvalStatusID = 3
		}

		articleApprovalStepLogsRes = &res.ArticleApprovalStepLogsDetailResponse{
			ID:                    articleApprovalStepLogsReq.ID,
			ArticleApprovalFlowID: articleApprovalStepLogsReq.ApprovalFlowId,
			WorkflowStepID:        uint(articleApprovalStepLogsReq.StepOrder),
			ApproverUserID:        articleApprovalStepLogsReq.ApprovedById,
			ApprovalStatusID:      approvalStatusID,
			Comments:              articleApprovalStepLogsReq.Message,
			ApprovedAt:            &articleApprovalStepLogsReq.ProcessedAt,
			IsAutoApproved:        false, // Default value
			CreatedAt:             articleApprovalStepLogsReq.CreatedAt,
			UpdatedAt:             articleApprovalStepLogsReq.CreatedAt, // Use CreatedAt as UpdatedAt
			// Relations would be populated separately if needed
		}
	}

	return articleApprovalStepLogsRes
}

func ArticleApprovalStepLogsSummaryResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	articleApprovalStepLogsReq *entity.ArticleApprovalStepLogs,
	usersRepo usersRepository.UsersRepository,
) (articleApprovalStepLogsRes *res.ArticleApprovalStepLogsSummaryResponse) {

	approverName := ""
	if articleApprovalStepLogsReq.ApprovedById != nil {
		findUser, _ := usersRepo.FindOne(clientId, *articleApprovalStepLogsReq.ApprovedById)
		if findUser != nil {
			approverName = findUser.Fullname
		}
	}

	if articleApprovalStepLogsReq != nil {
		// Map entity fields to response fields
		approvalStatusName := "Pending"
		if articleApprovalStepLogsReq.Action == "approved" {
			approvalStatusName = "Approved"
		} else if articleApprovalStepLogsReq.Action == "rejected" {
			approvalStatusName = "Rejected"
		}

		articleApprovalStepLogsRes = &res.ArticleApprovalStepLogsSummaryResponse{
			ID:                 articleApprovalStepLogsReq.ID,
			WorkflowStepID:     uint(articleApprovalStepLogsReq.StepOrder),
			StepName:           articleApprovalStepLogsReq.StepName,
			ApproverUserID:     articleApprovalStepLogsReq.ApprovedById,
			ApproverUserName:   &approverName,
			ApprovalStatusID:   uint(1), // Default status ID
			ApprovalStatusName: approvalStatusName,
			ApprovedAt:         &articleApprovalStepLogsReq.ProcessedAt,
			IsAutoApproved:     false, // Default value
		}
	}

	return articleApprovalStepLogsRes
}

func ApprovalHistoryResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	articleApprovalStepLogsReq *entity.ArticleApprovalStepLogs,
	usersRepo usersRepository.UsersRepository,
) (approvalHistoryRes *res.ApprovalHistoryResponse) {

	if articleApprovalStepLogsReq != nil {
		// Create a basic ApprovalHistoryResponse structure
		// This would typically be built from multiple step logs, not a single one
		approvalHistoryRes = &res.ApprovalHistoryResponse{
			ArticleID:    0,  // Would need article information
			ArticleTitle: "", // Would need article information
			WorkflowName: "", // Would need workflow information
			StartedAt:    articleApprovalStepLogsReq.CreatedAt,
			CompletedAt:  nil, // Would be set when workflow is complete
			CurrentStep:  &articleApprovalStepLogsReq.StepName,
			Steps:        []res.ArticleApprovalStepLogsSummaryResponse{}, // Would be populated with all steps
		}
	}

	return approvalHistoryRes
}
