package mapper

import (
	"web-qudo-be/app/database/entity"
	res "web-qudo-be/app/module/client_approval_settings/response"

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

func ClientApprovalSettingsResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	settings *entity.ClientApprovalSettings,
) *res.ClientApprovalSettingsResponse {
	if settings == nil {
		return nil
	}

	return &res.ClientApprovalSettingsResponse{
		ID:                       settings.ID,
		ClientId:                 settings.ClientId.String(),
		RequiresApproval:         *settings.RequiresApproval,
		DefaultWorkflowId:        settings.DefaultWorkflowId,
		AutoPublishArticles:      *settings.AutoPublishArticles,
		ApprovalExemptUsers:      settings.ApprovalExemptUsers,
		ApprovalExemptRoles:      settings.ApprovalExemptRoles,
		ApprovalExemptCategories: settings.ApprovalExemptCategories,
		RequireApprovalFor:       settings.RequireApprovalFor,
		SkipApprovalFor:          settings.SkipApprovalFor,
		IsActive:                 *settings.IsActive,
		CreatedAt:                settings.CreatedAt,
		UpdatedAt:                settings.UpdatedAt,
	}
}

func ClientApprovalSettingsDetailResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	settings *entity.ClientApprovalSettings,
) *res.ClientApprovalSettingsDetailResponse {
	if settings == nil {
		return nil
	}

	response := &res.ClientApprovalSettingsDetailResponse{
		ID:                       settings.ID,
		ClientId:                 settings.ClientId.String(),
		RequiresApproval:         *settings.RequiresApproval,
		DefaultWorkflowId:        settings.DefaultWorkflowId,
		AutoPublishArticles:      *settings.AutoPublishArticles,
		ApprovalExemptUsers:      settings.ApprovalExemptUsers,
		ApprovalExemptRoles:      settings.ApprovalExemptRoles,
		ApprovalExemptCategories: settings.ApprovalExemptCategories,
		RequireApprovalFor:       settings.RequireApprovalFor,
		SkipApprovalFor:          settings.SkipApprovalFor,
		IsActive:                 *settings.IsActive,
		CreatedAt:                settings.CreatedAt,
		UpdatedAt:                settings.UpdatedAt,
	}

	// Add client relation if available
	if settings.Client.ID != uuid.Nil {
		response.Client = &res.ClientResponse{
			ID:       1, // Placeholder - would need proper ID mapping
			Name:     settings.Client.Name,
			IsActive: *settings.Client.IsActive,
		}
	}

	// Add workflow relation if available
	if settings.Workflow != nil && settings.Workflow.ID != 0 {
		response.Workflow = &res.WorkflowResponse{
			ID:          settings.Workflow.ID,
			Name:        settings.Workflow.Name,
			Description: settings.Workflow.Description,
			IsActive:    *settings.Workflow.IsActive,
		}
	}

	return response
}

func ApprovalStatusResponseMapper(
	log zerolog.Logger,
	clientId *uuid.UUID,
	settings *entity.ClientApprovalSettings,
) *res.ApprovalStatusResponse {
	if settings == nil {
		return &res.ApprovalStatusResponse{
			RequiresApproval:    true, // Default to requiring approval
			AutoPublishArticles: false,
			IsActive:            false,
			Message:             "No approval settings found",
		}
	}

	return &res.ApprovalStatusResponse{
		RequiresApproval:    *settings.RequiresApproval,
		AutoPublishArticles: *settings.AutoPublishArticles,
		IsActive:            *settings.IsActive,
		Message:             "Approval settings loaded successfully",
	}
}
