<?php

namespace FleekDash\API\Comments;

/**
 * Comments Query Builder
 *
 * Handles building \get_comments() arguments
 * Extracted from FLEEKDASH_Comments_Optimized_API for better organization
 *
 * @package FleekDash\API\Comments
 * @since 2.2.0
 */
class CommentsQueryBuilder
{
    /**
     * Build complete query arguments
     *
     * @param array $params Request parameters
     * @return array \get_comments() arguments
     */
    public function buildQueryArgs($params)
    {
        $per_page = $params['per_page'] ?? 100;
        $page = $params['page'] ?? 1;

        $args = [
            'number' => $per_page,
            'offset' => ($page - 1) * $per_page,
            'orderby' => $params['orderby'] ?? 'comment_date',
            'order' => $params['order'] ?? 'DESC',
        ];

        // Apply status filter
        $args = $this->handleStatusFilter($args, $params);

        return $args;
    }

    /**
     * Handle comment status filtering
     *
     * @param array $args Current query args
     * @param array $params Request parameters
     * @return array Modified query args
     */
    public function handleStatusFilter($args, $params)
    {
        $status = $params['status'] ?? 'all';

        if ($status !== 'all') {
            // Map frontend status to WordPress status
            $status_mapping = [
                'approved' => 'approve',
                'pending' => 'hold',
                'spam' => 'spam',
                'trash' => 'trash',
            ];

            $args['status'] = $status_mapping[$status] ?? $status;
        } else {
            // Get all available statuses except spam and trash
            $args['status'] = ['approve', 'hold', '0'];
        }

        return $args;
    }

    /**
     * Add post ID filter to query args
     *
     * @param array $args Current query args
     * @param int $post_id Post ID
     * @return array Modified query args
     */
    public function addPostFilter($args, $post_id)
    {
        if ($post_id > 0) {
            $args['post_id'] = $post_id;
        }

        return $args;
    }

    /**
     * Add author filter to query args
     *
     * @param array $args Current query args
     * @param int $author_id Author ID
     * @return array Modified query args
     */
    public function addAuthorFilter($args, $author_id)
    {
        if ($author_id > 0) {
            $args['user_id'] = $author_id;
        }

        return $args;
    }

    /**
     * Add date range filter
     *
     * @param array $args Current query args
     * @param string $date_from Start date (Y-m-d format)
     * @param string $date_to End date (Y-m-d format)
     * @return array Modified query args
     */
    public function addDateRangeFilter($args, $date_from = null, $date_to = null)
    {
        if ($date_from) {
            $args['date_query'][] = [
                'after' => $date_from,
                'inclusive' => true,
            ];
        }

        if ($date_to) {
            $args['date_query'][] = [
                'before' => $date_to,
                'inclusive' => true,
            ];
        }

        return $args;
    }

    /**
     * Build response with pagination metadata
     *
     * @param array $comments Formatted comments array
     * @param int $page Current page
     * @param int $per_page Comments per page
     * @param int $total_comments Total comments (optional)
     * @return array Response structure
     */
    public function buildResponse($comments, $page, $per_page, $total_comments = null)
    {
        $response = [
            'success' => true,
            'data' => $comments,
            'pagination' => [
                'page' => $page,
                'per_page' => $per_page,
                'total' => count($comments),
            ],
        ];

        if ($total_comments !== null) {
            $response['pagination']['total_comments'] = $total_comments;
            $response['pagination']['total_pages'] = ceil($total_comments / $per_page);
        }

        return $response;
    }
}

