<?php

namespace SaveToWishlist\Traits;

/**
 * Trait API_Permissions
 *
 * This trait handles the permissions for the API.
 */
trait API_Permissions {
    /**
     * Check if the user has the required permissions.
     *
     * @since 1.0.0
     * @access public
     * @param \WP_REST_Request $request The request object.
     * @return bool|\WP_Error
     */
    public function check_admin_permission( \WP_REST_Request $request ) {
        $response = true;

        if ( ! current_user_can( 'manage_options' ) ) {
            $response = new \WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit settings options.', 'saveto-wishlist-lite-for-woocommerce' ), array( 'status' => \rest_authorization_required_code() ) );
        }

        return apply_filters( 'stwlite_api_admin_permissions_check', $response, $request );
    }

    /**
     * Check if the user has the required permissions.
     *
     * @since 1.0.0
     * @access public
     * @param \WP_REST_Request $request The request object.
     * @return bool|\WP_Error
     */
    public function check_user_permission( \WP_REST_Request $request ) {
        $response = true;

        // Allow all logged-in users (including customers) to access frontend endpoints.
        if ( ! is_user_logged_in() ) {
            // Check if the collection is publicly accessible.
            $collection_id = (int) $request->get_param( 'id' );

            if ( ! empty( $collection_id ) ) {
                // Get the collection to check if it's public.
                $collection = \SaveToWishlist\Classes\Factories\Collections::instance()->get_collection( $collection_id );
                $data       = array();
                if ( $collection ) {
                    $data = \SaveToWishlist\Classes\Factories\Collection::get_instance( $collection );
                }

                // Filter the validation.
                $validation = apply_filters( 'stwlite_check_any_user_has_permission_validation', false, $data, $request );
                if ( $validation ) {
                    return apply_filters( 'stwlite_api_user_permissions_check', true, $request );
                }
            }

            // If pass through the filter, allow access.
            $force_access = apply_filters( 'stwlite_api_user_force_access', false, $request );
            if ( $force_access ) {
                return apply_filters( 'stwlite_api_user_permissions_check', true, $request );
            }

            $response = new \WP_Error( 'rest_forbidden_context', __( 'Sorry, you must be logged in to access this endpoint.', 'saveto-wishlist-lite-for-woocommerce' ), array( 'status' => \rest_authorization_required_code() ) );
        }

        return apply_filters( 'stwlite_api_user_permissions_check', $response, $request );
    }

    /**
     * Checks if a given request has read permission.
     *
     * @since 1.0.0
     * @access public
     *
     * @param \WP_REST_Request $request Full details about the request.
     * @return true|\WP_Error True if the request has read access, WP_Error object otherwise.
     */
    public function get_read_permissions_check( $request ) {
        if ( ! current_user_can( 'delete_posts' ) ) {
            return new \WP_Error(
                'rest_forbidden_context',
                __( 'Sorry, you are not allowed access to this endpoint.', 'saveto-wishlist-lite-for-woocommerce' ),
                array( 'status' => \rest_authorization_required_code() )
            );
        }

        return apply_filters( 'stwlite_get_read_permissions_check', true, $request );
    }

    /**
     * Checks if a given request has publish permission.
     *
     * @since 1.0.0
     * @access public
     *
     * @param \WP_REST_Request $request Full details about the request.
     * @return true|\WP_Error True if the request has read access, WP_Error object otherwise.
     */
    public function get_publish_permission_check( $request ) {
        if ( ! current_user_can( 'delete_others_posts' ) ) {
            return new \WP_Error(
                'rest_forbidden_context',
                __( 'Sorry, you are not allowed access to this endpoint.', 'saveto-wishlist-lite-for-woocommerce' ),
                array( 'status' => \rest_authorization_required_code() )
            );
        }

        return apply_filters( 'stwlite_get_publish_permissions_check', true, $request );
    }

    /**
     * Check if any user has permission.
     *
     * @since 1.0.0
     * @access public
     *
     * @param \WP_REST_Request $request The request object.
     * @return bool True if user is guest or logged-in.
     */
    public function check_any_user_has_permission( $request ) {
        if ( ! is_user_logged_in() ) {
            // Check if the collection is publicly accessible.
            $collection_id = $request->get_param( 'collection_id' );

            if ( ! empty( $collection_id ) ) {
                // Get the collection to check if it's public.
                $collection = \SaveToWishlist\Classes\Factories\Collections::instance()->get_collection( $collection_id );
                $data       = array();
                if ( $collection ) {
                    $data = \SaveToWishlist\Classes\Factories\Collection::get_instance( $collection );
                }

                // If collection exists and is public, allow access.
                if ( ! empty( $data ) && ! empty( $data->is_public ) ) {
                    return apply_filters( 'stwlite_check_any_user_has_permission', true, $request );
                }

                // Filter the validation.
                $validation = apply_filters( 'stwlite_check_any_user_has_permission_validation', false, $data, $request );
                if ( $validation ) {
                    return apply_filters( 'stwlite_check_any_user_has_permission', true, $request );
                }
            }

            return new \WP_Error(
                'rest_forbidden_context',
                __( 'Sorry, you are not allowed access to this endpoint.', 'saveto-wishlist-lite-for-woocommerce' ),
                array( 'status' => \rest_authorization_required_code() )
            );
        }

        return apply_filters( 'stwlite_check_any_user_has_permission', true, $request );
    }

    /**
     * Check if guest user has permission.
     *
     * @since 1.0.0
     * @access public
     *
     * @param \WP_REST_Request $request The request object.
     * @return bool True if user is guest or logged-in.
     */
    public function check_guest_permission( $request ) {

        if ( is_user_logged_in() ) {
            return new \WP_Error(
                'rest_forbidden_context',
                __( 'Sorry, you are not allowed access to this endpoint.', 'saveto-wishlist-lite-for-woocommerce' ),
                array( 'status' => \rest_authorization_required_code() )
            );
        }

        return apply_filters( 'stwlite_check_guest_permission', true, $request );
    }
}
