<?php
/**
 * JsonEndpoint.php
 *
 * @since      1.0.0
 * @author     alfiopiccione <alfio.piccione@gmail.com>

 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 POP
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace WcElectronInvoice\EndPoint;

if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

use WcElectronInvoice\Admin\Settings\OptionPage;
use WcElectronInvoice\Functions as F;

/**
 * Class Endpoints
 *
 * @since  1.0.0
 * @author alfiopiccione <alfio.piccione@gmail.com>
 */
class Endpoints implements EndpointsInterface
{
    /**
     * Api endpoint
     *
     * New endpoint: 'wp-admin/admin.php' - since 6.0.0
     *
     * Old endpoint: 'wc-inv' - since 1.0.0
     *
     * @since 6.0.0
     *
     * @var string
     */
    const ENDPOINT = 'wp-admin/admin.php';

    /**
     * Current language
     *
     * @since 1.0.0
     *
     * @var string
     */
    public string $lang;

    /**
     * Query var
     *
     * @since 1.0.0
     *
     * @var string
     */
    public string $var;

    /**
     * Post Type
     *
     * @since 1.0.0
     *
     * @var string The post type for this json
     */
    public $postType = array('shop_order');

    /**
     * JsonEndpoint constructor.
     *
     */
    public function __construct()
    {
        $this->lang = F\getCurrentLanguage();
        $this->var  = 'wc-elc-inv';
    }

    /**
     * Add EndPoint
     * - method no longer used since version 6.0.0 endpoint moved to wp-admin
     *
     * @return null
     */
    public function addEndPoint()
    {
        $getKey = F\filterInput($_GET, 'key', FILTER_UNSAFE_RAW);
        $format = F\filterInput($_GET, 'format', FILTER_UNSAFE_RAW);

        /**
         * Filter add_rewrite_rule post types
         */
        $this->postType = apply_filters('wc_el_inv-types_add_rewrite_role', $this->postType);

        // Esc if empty post type & endpoint
        if (null === $this->postType && null === self::ENDPOINT) {
            return null;
        }

        if (is_array($this->postType) && sizeof($this->postType) >= 0) {
            // Multi post type
            foreach ($this->postType as $type) {
                $this->addRewriteRule(self::ENDPOINT, (string)$type);
            }
        } else {
            $this->addRewriteRule(self::ENDPOINT, (string)$this->postType);
        }

        if ($getKey && $format) {
            flush_rewrite_rules(false);
        }
    }

    /**
     * Add Query Vars
     * - method no longer used since version 6.0.0 endpoint moved to wp-admin
     *
     * @param $vars
     *
     * @return array
     * @since 1.0.0
     *
     */
    public function addQueryVars($vars)
    {
        $vars[] = $this->var;

        return $vars;
    }

    /**
     * Add Rewrite Rule
     * - method no longer used since version 6.0.0 endpoint moved to wp-admin
     *
     * @param $endpoint
     * @param $postType
     *
     * @since 1.0.0
     *
     */
    public function addRewriteRule($endpoint, $postType)
    {
        // Add new rewrite tags to WP for our endpoint's post_type
        add_rewrite_tag('%' . $postType . '%', '([^&]+)');

        // and post_id arguments
        add_rewrite_tag('%' . $postType . '_id%', '([0-9]+)');

        // general rule finds "all" (post_per_page) of a given post_type
        add_rewrite_rule(
            '^' . $endpoint . '/([^&]+)/?$',
            'index.php?' . $postType . '=$matches[1]&' . $this->var . '=yes',
            'top'
        );

        // specific rule finds a single post
        add_rewrite_rule(
            '^' . $endpoint . '/([^&]+)/([0-9]+)/?$',
            'index.php?' . $postType . '=$matches[1]&' . $postType . '_id=$matches[2]&' . $this->var . '=yes',
            'top'
        );
    }

    /**
     * Add Rewrite Endpoint
     * - method no longer used since version 6.0.0 endpoint moved to wp-admin
     *
     * @since 1.0.0
     */
    public function addRewriteEndpoint()
    {
        add_rewrite_endpoint(self::ENDPOINT, EP_PAGES);
    }

    /**
     * Set Query Args
     * - method no longer used since version 6.0.0 endpoint moved to wp-admin
     *
     * @return null
     * @since 1.0.0
     *
     */
    public function setQueryArgs()
    {
        $query = F\getWpQuery();

        $idsArgs            = F\filterInput($_GET, 'get_ids', FILTER_UNSAFE_RAW);
        $format             = F\filterInput($_GET, 'format', FILTER_UNSAFE_RAW);
        $dateOrderCompleted = F\filterInput($_GET, 'date_completed', FILTER_UNSAFE_RAW);
        $dateOrderModified  = F\filterInput($_GET, 'date_modified', FILTER_UNSAFE_RAW);
        $dateOrderIN        = F\filterInput($_GET, 'date_in', FILTER_UNSAFE_RAW);
        $dateOrderOUT       = F\filterInput($_GET, 'date_out', FILTER_UNSAFE_RAW);
        $provider           = F\filterInput($_GET, 'provider', FILTER_UNSAFE_RAW);

        if (! $query) {
            return null;
        }

        if ($idsArgs) {
            $query->set('get_ids', 'true');
        }

        // Set date completed timestamp
        if ($dateOrderCompleted) {
            $query->set('date_completed', $dateOrderCompleted);
        }

        // Set date modified timestamp
        if ($dateOrderModified) {
            $query->set('date_modified', $dateOrderModified);
        }

        // Set date completed in timestamp
        if ($dateOrderIN) {
            $query->set('date_in', $dateOrderIN);
        }

        // Set date completed out timestamp
        if ($dateOrderOUT) {
            $query->set('date_out', $dateOrderOUT);
        }

        // Set format
        if ($format) {
            $query->set('format', $format);
        }

        if ('prod' === WC_EL_INV_ENV) {
            $key = OptionPage::getK();
            $query->set('key', $key);
        }

        // Set provider
        if ($provider) {
            $query->set('provider', $provider);
        }
    }
}
