package controller

import (
	"fmt"
	"netidhub-saas-be/app/module/article_approval_flows/request"
	"netidhub-saas-be/app/module/article_approval_flows/service"
	usersRepository "netidhub-saas-be/app/module/users/repository"
	"netidhub-saas-be/utils/paginator"
	utilSvc "netidhub-saas-be/utils/service"
	"strconv"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog"

	utilRes "netidhub-saas-be/utils/response"
	utilVal "netidhub-saas-be/utils/validator"
)

type articleApprovalFlowsController struct {
	articleApprovalFlowsService service.ArticleApprovalFlowsService
	UsersRepo                   usersRepository.UsersRepository
	Log                         zerolog.Logger
}

type ArticleApprovalFlowsController interface {
	All(c *fiber.Ctx) error
	Show(c *fiber.Ctx) error
	SubmitForApproval(c *fiber.Ctx) error
	Approve(c *fiber.Ctx) error
	Resubmit(c *fiber.Ctx) error
	GetMyApprovalQueue(c *fiber.Ctx) error
	GetPendingApprovals(c *fiber.Ctx) error
	GetApprovalHistory(c *fiber.Ctx) error
	GetDashboardStats(c *fiber.Ctx) error
	GetWorkloadStats(c *fiber.Ctx) error
	GetApprovalAnalytics(c *fiber.Ctx) error

	// Multi-branch support methods
	ProcessMultiBranchApproval(c *fiber.Ctx) error
	GetNextStepsPreview(c *fiber.Ctx) error

	// Simplified approval by article ID
	ApproveArticleByFlow(c *fiber.Ctx) error
}

func NewArticleApprovalFlowsController(articleApprovalFlowsService service.ArticleApprovalFlowsService, usersRepo usersRepository.UsersRepository, log zerolog.Logger) ArticleApprovalFlowsController {
	return &articleApprovalFlowsController{
		articleApprovalFlowsService: articleApprovalFlowsService,
		UsersRepo:                   usersRepo,
		Log:                         log,
	}
}

// All ArticleApprovalFlows
// @Summary      Get all ArticleApprovalFlows
// @Description  API for getting all ArticleApprovalFlows
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        req query request.ArticleApprovalFlowsQueryRequest false "query parameters"
// @Param        req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows [get]
func (_i *articleApprovalFlowsController) All(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	reqContext := request.ArticleApprovalFlowsQueryRequestContext{
		ArticleId:   c.Query("articleId"),
		WorkflowId:  c.Query("workflowId"),
		StatusId:    c.Query("statusId"),
		SubmittedBy: c.Query("submittedBy"),
		CurrentStep: c.Query("currentStep"),
		DateFrom:    c.Query("dateFrom"),
		DateTo:      c.Query("dateTo"),
	}
	req := reqContext.ToParamRequest()
	req.Pagination = paginate

	// Get Authorization token from header
	authToken := c.Get("Authorization")

	_i.Log.Info().Interface("authToken", authToken).Msg("")

	articleApprovalFlowsData, paging, err := _i.articleApprovalFlowsService.GetAll(authToken, req)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ArticleApprovalFlows list successfully retrieved"},
		Data:     articleApprovalFlowsData,
		Meta:     paging,
	})
}

// Show ArticleApprovalFlows
// @Summary      Get one ArticleApprovalFlows
// @Description  API for getting one ArticleApprovalFlows
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        id path int true "ArticleApprovalFlows ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/{id} [get]
func (_i *articleApprovalFlowsController) Show(c *fiber.Ctx) error {
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid ID format")
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")

	articleApprovalFlowsData, err := _i.articleApprovalFlowsService.FindOne(authToken, uint(id))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"ArticleApprovalFlows successfully retrieved"},
		Data:     articleApprovalFlowsData,
	})
}

// SubmitForApproval ArticleApprovalFlows
// @Summary      Submit article for approval
// @Description  API for submitting article for approval
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param        req body request.SubmitForApprovalRequest true "Submit for approval data"
// @Success      201  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/submit [post]
func (_i *articleApprovalFlowsController) SubmitForApproval(c *fiber.Ctx) error {
	req := new(request.SubmitForApprovalRequest)
	if err := c.BodyParser(req); err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid request body")
	}

	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	// Get Authorization token from header and extract user ID
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	var workflowId *uint
	if req.WorkflowId != nil {
		workflowIdVal := uint(*req.WorkflowId)
		workflowId = &workflowIdVal
	}
	articleApprovalFlowsData, err := _i.articleApprovalFlowsService.SubmitArticleForApproval(authToken, uint(req.ArticleId), user.ID, workflowId)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Article successfully submitted for approval"},
		Data:     articleApprovalFlowsData,
	})
}

// Approve ArticleApprovalFlows
// @Summary      Approve article
// @Description  API for approving article
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param        id path int true "ArticleApprovalFlows ID"
// @Param        req body request.ApprovalActionRequest true "Approval action data"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/{id}/approve [put]
func (_i *articleApprovalFlowsController) Approve(c *fiber.Ctx) error {
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid ID format")
	}

	req := new(request.ApprovalActionRequest)
	if err := c.BodyParser(req); err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid request body")
	}

	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	err = _i.articleApprovalFlowsService.ApproveStep(authToken, uint(id), user.ID, req.Message)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Article successfully approved"},
		Data:     nil,
	})
}

// Resubmit ArticleApprovalFlows
// @Summary      Resubmit article after revision
// @Description  API for resubmitting article after revision
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param        id path int true "ArticleApprovalFlows ID"
// @Param        req body request.ResubmitRequest true "Resubmit data"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/{id}/resubmit [put]
func (_i *articleApprovalFlowsController) Resubmit(c *fiber.Ctx) error {
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid ID format")
	}

	req := new(request.ResubmitRequest)
	if err := c.BodyParser(req); err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid request body")
	}

	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	err = _i.articleApprovalFlowsService.ResubmitAfterRevision(authToken, uint(id), user.ID)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Article successfully resubmitted"},
		Data:     nil,
	})
}

// GetMyApprovalQueue ArticleApprovalFlows
// @Summary      Get my approval queue
// @Description  API for getting my approval queue
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param        includePreview query bool false "Include article preview"
// @Param        urgentOnly query bool false "Show only urgent articles"
// @Param        req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/my-queue [get]
func (_i *articleApprovalFlowsController) GetMyApprovalQueue(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	// Optional parameters
	includePreview := c.QueryBool("includePreview", false)
	urgentOnly := c.QueryBool("urgentOnly", false)

	approvalQueueData, paging, err := _i.articleApprovalFlowsService.GetMyApprovalQueue(authToken, user.UserLevelId, paginate.Page, paginate.Limit, includePreview, urgentOnly)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"My approval queue successfully retrieved"},
		Data:     approvalQueueData,
		Meta:     paging,
	})
}

// GetPendingApprovals ArticleApprovalFlows
// @Summary      Get pending approvals
// @Description  API for getting pending approvals
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param        req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/pending [get]
func (_i *articleApprovalFlowsController) GetPendingApprovals(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	filters := make(map[string]interface{})
	pendingApprovalsData, paging, err := _i.articleApprovalFlowsService.GetPendingApprovals(authToken, user.UserLevelId, paginate.Page, paginate.Limit, filters)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Pending approvals successfully retrieved"},
		Data:     pendingApprovalsData,
		Meta:     paging,
	})
}

// GetApprovalHistory ArticleApprovalFlows
// @Summary      Get approval history
// @Description  API for getting approval history
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        articleId query int false "Article ID filter"
// @Param        userId query int false "User ID filter"
// @Param        req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/history [get]
func (_i *articleApprovalFlowsController) GetApprovalHistory(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	articleId := 0
	if articleIdStr := c.Query("articleId"); articleIdStr != "" {
		articleId, err = strconv.Atoi(articleIdStr)
		if err != nil {
			return utilRes.ErrorBadRequest(c, "Invalid articleId format")
		}
	}

	// userId parameter is not used in the current implementation
	// userId := 0
	// if userIdStr := c.Query("userId"); userIdStr != "" {
	//	userId, err := strconv.Atoi(userIdStr)
	//	if err != nil {
	//		return utilRes.ErrorBadRequest(c, "Invalid userId format")
	//	}
	// }

	// Get Authorization token from header
	authToken := c.Get("Authorization")

	approvalHistoryData, paging, err := _i.articleApprovalFlowsService.GetApprovalHistory(authToken, uint(articleId), paginate.Page, paginate.Limit)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Approval history successfully retrieved"},
		Data:     approvalHistoryData,
		Meta:     paging,
	})
}

// GetDashboardStats ArticleApprovalFlows
// @Summary      Get dashboard statistics
// @Description  API for getting dashboard statistics
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/dashboard-stats [get]
func (_i *articleApprovalFlowsController) GetDashboardStats(c *fiber.Ctx) error {
	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	// TODO: Implement GetDashboardStats method in service
	_ = user.UserLevelId // suppress unused variable warning
	// dashboardStatsData, err := _i.articleApprovalFlowsService.GetDashboardStats(authToken, user.UserLevelId)
	// if err != nil {
	//	return err
	// }

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Dashboard statistics successfully retrieved"},
		Data:     nil,
	})
}

// GetWorkloadStats ArticleApprovalFlows
// @Summary      Get workload statistics
// @Description  API for getting workload statistics
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/workload-stats [get]
func (_i *articleApprovalFlowsController) GetWorkloadStats(c *fiber.Ctx) error {
	// Get Authorization token from header
	// authToken := c.Get("Authorization")

	// TODO: Implement GetWorkloadStats method in service
	// workloadStatsData, err := _i.articleApprovalFlowsService.GetWorkloadStats(authToken)
	// if err != nil {
	//	return err
	// }

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Workload statistics successfully retrieved"},
		Data:     nil,
	})
}

// GetApprovalAnalytics ArticleApprovalFlows
// @Summary      Get approval analytics
// @Description  API for getting approval analytics
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        Authorization header string false "Insert your access token" default(Bearer <Add access token here>)
// @Param        period query string false "Period filter (daily, weekly, monthly)"
// @Param        startDate query string false "Start date filter (YYYY-MM-DD)"
// @Param        endDate query string false "End date filter (YYYY-MM-DD)"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/analytics [get]
func (_i *articleApprovalFlowsController) GetApprovalAnalytics(c *fiber.Ctx) error {
	// period := c.Query("period", "monthly")
	// startDate := c.Query("startDate")
	// endDate := c.Query("endDate")

	// Get Authorization token from header
	// authToken := c.Get("Authorization")

	// TODO: Implement GetApprovalAnalytics method in service
	// analyticsData, err := _i.articleApprovalFlowsService.GetApprovalAnalytics(authToken, period, startDate, endDate)
	// if err != nil {
	//	return err
	// }

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Approval analytics successfully retrieved"},
		Data:     nil,
	})
}

// ProcessMultiBranchApproval ArticleApprovalFlows
// @Summary      Process multi-branch approval
// @Description  API for processing multi-branch approval with conditional routing
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        id path int true "ArticleApprovalFlows ID"
// @Param        req body request.ApprovalActionRequest true "Approval action data"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/{id}/multi-branch-approve [post]
func (_i *articleApprovalFlowsController) ProcessMultiBranchApproval(c *fiber.Ctx) error {
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid ID format")
	}

	req := new(request.ApprovalActionRequest)
	if err := c.BodyParser(req); err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid request body")
	}

	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	err = _i.articleApprovalFlowsService.ProcessMultiBranchApproval(authToken, uint(id), user.ID, req.Message)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Article successfully processed through multi-branch approval"},
		Data:     nil,
	})
}

// GetNextStepsPreview ArticleApprovalFlows
// @Summary      Get next steps preview for multi-branch workflow
// @Description  API for getting preview of next steps based on submitter's user level
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        id path int true "ArticleApprovalFlows ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/{id}/next-steps-preview [get]
func (_i *articleApprovalFlowsController) GetNextStepsPreview(c *fiber.Ctx) error {
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid ID format")
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	// Get current flow to determine submitter's level
	flow, err := _i.articleApprovalFlowsService.FindOne(authToken, uint(id))
	if err != nil {
		return err
	}

	// Get submitter's user level
	submitterLevelId, err := _i.articleApprovalFlowsService.GetUserLevelId(authToken, flow.SubmittedById)
	if err != nil {
		return err
	}

	// Get next steps based on submitter's level
	nextSteps, err := _i.articleApprovalFlowsService.FindNextStepsForBranch(authToken, flow.WorkflowId, flow.CurrentStep, submitterLevelId)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Next steps preview successfully retrieved"},
		Data: map[string]interface{}{
			"current_step":       flow.CurrentStep,
			"submitter_level_id": submitterLevelId,
			"next_steps":         nextSteps,
			"total_next_steps":   len(nextSteps),
		},
	})
}

// ApproveArticleByFlow approves an article using its active approval flow
// @Summary      Approve article by its active approval flow
// @Description  API for approving an article using its currently active approval flow (simplified version)
// @Tags         ArticleApprovalFlows
// @Security     Bearer
// @Param        Authorization header string true "Insert the Authorization"
// @Param        articleId path int true "Article ID"
// @Param        payload body request.ApprovalActionRequest true "Required payload"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /article-approval-flows/articles/{articleId}/approve [post]
func (_i *articleApprovalFlowsController) ApproveArticleByFlow(c *fiber.Ctx) error {
	articleId, err := strconv.Atoi(c.Params("articleId"))
	if err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid article ID format")
	}

	req := new(request.ApprovalActionRequest)
	if err := c.BodyParser(req); err != nil {
		return utilRes.ErrorBadRequest(c, "Invalid request body")
	}

	if err := utilVal.ParseAndValidate(c, req); err != nil {
		return err
	}

	// Get Authorization token from header
	authToken := c.Get("Authorization")
	if authToken == "" {
		return utilRes.ErrorBadRequest(c, "Authorization token required")
	}

	user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	if user == nil {
		return utilRes.ErrorBadRequest(c, "Invalid authorization token")
	}

	// Find active approval flow for the article
	activeFlow, err := _i.articleApprovalFlowsService.FindActiveByArticleId(uint(articleId))
	if err != nil {
		return utilRes.Resp(c, utilRes.Response{
			Success:  false,
			Messages: utilRes.Messages{"No active approval flow found for this article"},
		})
	}

	// Process approval using multi-branch logic
	err = _i.articleApprovalFlowsService.ProcessApprovalAction(authToken, activeFlow.ID, req.Action, user.ID, req.Message)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{fmt.Sprintf("Article successfully %s through active approval flow", req.Action)},
		Data: map[string]interface{}{
			"article_id":     articleId,
			"flow_id":        activeFlow.ID,
			"action":         req.Action,
			"current_step":   activeFlow.CurrentStep,
			"current_branch": activeFlow.CurrentBranch,
			"workflow_id":    activeFlow.WorkflowId,
		},
	})
}
