<?php

/**
 * Class CloudRepository
 */
class CloudRepository
{
    /**
     * @var null|CloudRepository
     */
    private static $instance = null;

    /**
     * @return CloudRepository
     */
    public static function getInstance(): CloudRepository
    {
        if (self::$instance == null) {
            self::$instance = new CloudRepository();
        }

        return self::$instance;
    }

    /**
     * @param string $metaCompare
     * @param int $postPerPage
     * @return WP_Query
     */
    public static function getAttachments(string $metaCompare, int $postPerPage = -1): WP_Query
    {
        $settings = get_option(UPCASTED_S3_OFFLOAD_SETTINGS, []);
        $mimeTypes = isset($settings[UPCASTED_S3_OFFLOAD_INCLUDED_FILETYPES]) ? $settings[UPCASTED_S3_OFFLOAD_INCLUDED_FILETYPES] : [];
        
        $params = [
            'post_type' => ['attachment'],
            'post_status' => 'inherit',
            'fields' => 'ids',
            'posts_per_page' => $postPerPage,
            'no_found_rows' => false // Enable found_posts calculation for counts
        ];
        
        // Only add MIME type filter if we have specific types configured
        if (is_array($mimeTypes) && !empty($mimeTypes)) {
            $params['post_mime_type'] = $mimeTypes;
        }
        // If no MIME types specified, include all attachment types
        // Base metadata conditions by direction
        if ($metaCompare == 'LIKE') {
            $params['meta_key'] = '_wp_attachment_metadata';
            $params['meta_value'] = 'bucket';
            $params['meta_compare'] = $metaCompare;
        } elseif ($metaCompare == 'NOT LIKE') {
            $params['meta_query'] = array(
                'relation' => 'OR',
                array(
                    'key' => '_wp_attachment_metadata',
                    'value' => 'bucket',
                    'compare' => 'NOT EXISTS',
                ),
                array(
                    'key' => '_wp_attachment_metadata',
                    'value' => 'bucket',
                    'compare' => 'NOT LIKE',
                )
            );
        }
        // Exclude permanent failures from automatic batch processing
        $permanentFailClause = array(
            'key' => '_upcasted_permanent_fail',
            'compare' => 'NOT EXISTS'
        );
        if (isset($params['meta_query'])) {
            $params['meta_query'] = array(
                'relation' => 'AND',
                $params['meta_query'],
                $permanentFailClause
            );
        } else {
            // If using simple meta_key / meta_value, convert to meta_query first
            if (isset($params['meta_key'])) {
                $orig = array(
                    'key' => $params['meta_key'],
                    'value' => $params['meta_value'],
                    'compare' => $params['meta_compare']
                );
                unset($params['meta_key'], $params['meta_value'], $params['meta_compare']);
                $params['meta_query'] = array(
                    'relation' => 'AND',
                    $orig,
                    $permanentFailClause
                );
            } else {
                $params['meta_query'] = array($permanentFailClause);
            }
        }
        return new WP_Query($params);
    }
}