<?php

namespace Wpcafe_Multivendor\Core\Hooks;

defined( "ABSPATH" ) || exit;

use WpCafe\Traits\Wpc_Singleton;

/**
 * Handles all product category-related vendor permissions and filtering for wpcafe controllers
 * using filter hooks to maintain clean separation between plugins.
 */
class Product_Category_Hooks {

    use Wpc_Singleton;

    /**
     * Initialize all product category filters
     */
    public function init() {
        add_filter( 'wpcafe_product_category_read_permission', [ $this, 'can_read_categories' ], 10, 2 );
        add_filter( 'wpcafe_product_category_query_args', [ $this, 'filter_category_query' ], 10, 2 );
        add_filter( 'wpcafe_product_category_item_permission', [ $this, 'can_access_category_item' ], 10, 2 );
    }

    /**
     * Check if user is admin
     *
     * @return bool
     */
    private function is_admin() {
        return current_user_can('manage_options');
    }

    /**
     * Check if user is vendor but not admin
     *
     * @return bool
     */
    private function is_vendor_only() {
        return wpc_user_is_dokan_vendor() && ! $this->is_admin();
    }

    /**
     * Filter category query args for vendors
     *
     * @param array $args Query arguments
     * @param \WP_REST_Request $request Request object
     * @return array
     */
    public function filter_category_query($args, $request) {
        if ( ! $this->is_vendor_only() ) {
            return $args;
        }

        // Get all product IDs owned by the vendor
        $vendor_product_ids = wc_get_products([
            'author' => get_current_user_id(),
            'limit' => -1,
            'status' => 'publish',
            'return' => 'ids',
        ]);

        if ( empty( $vendor_product_ids ) ) {
            $args['include'] = [0]; // Return args that will result in no categories
        } else {
            $args['object_ids'] = $vendor_product_ids; // Use object_ids to filter categories by vendor's products at query level
        }

        return $args;
    }

    /**
     * Check if user can read categories
     *
     * @param bool $default_permission Default permission
     * @param \WP_REST_Request $request Request object
     * @return bool
     */
    public function can_read_categories($default_permission, $request) {
        return $default_permission || wpc_user_is_dokan_vendor();
    }

    /**
     * Check if user can access specific category item
     *
     * @param bool $default_permission Default permission
     * @param \WP_REST_Request $request Request object
     * @return bool
     */
    public function can_access_category_item($default_permission, $request) {
        if ( $this->is_admin() ) {
            return true;
        }

        if ( ! wpc_user_is_dokan_vendor() ) {
            return $default_permission;
        }

        $id = intval( $request['id'] );
        $category = get_term( $id, 'product_cat' );

        if ( ! $category || is_wp_error( $category ) ) {
            return false;
        }

        // Get vendor's product IDs
        $vendor_product_ids = wc_get_products([
            'author' => get_current_user_id(),
            'limit' => -1,
            'status' => 'publish',
            'return' => 'ids',
        ]);

        if ( empty( $vendor_product_ids ) ) {
            return false;
        }

        // Check if category has any products from vendor
        $category_products = wc_get_products([
            'category' => [$category->slug],
            'include' => $vendor_product_ids,
            'limit' => 1,
            'return' => 'ids',
        ]);

        return ! empty( $category_products );
    }
}
