<?php
namespace BLR\Backline\Core;

if (!defined('ABSPATH')) {
    exit;
}

final class Request {
    public static function method(): string {
        return strtoupper(sanitize_text_field((string) wp_unslash($_SERVER['REQUEST_METHOD'] ?? 'GET')));
    }

    public static function query_absint(string $key, int $default = 0): int {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query arg.
        if (!isset($_GET[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query arg.
        return absint((string) wp_unslash($_GET[$key]));
    }

    public static function query_text(string $key, string $default = ''): string {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query arg.
        if (!isset($_GET[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query arg.
        return sanitize_text_field((string) wp_unslash($_GET[$key]));
    }

    public static function query_key(string $key, string $default = ''): string {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query arg.
        if (!isset($_GET[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only query arg.
        return sanitize_key((string) wp_unslash($_GET[$key]));
    }

    public static function query_choice(string $key, array $choices, string $default = ''): string {
        $value = self::query_key($key, $default);
        return in_array($value, $choices, true) ? $value : $default;
    }

    public static function post_text(string $key, string $default = ''): string {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        if (!isset($_POST[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        return sanitize_text_field((string) wp_unslash($_POST[$key]));
    }

    public static function post_kses(string $key, string $default = ''): string {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        if (!isset($_POST[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        return wp_kses_post(wp_unslash($_POST[$key]));
    }


    public static function post_absint(string $key, int $default = 0): int {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        if (!isset($_POST[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        return absint(wp_unslash($_POST[$key]));
    }

    public static function post_email(string $key, string $default = ''): string {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        if (!isset($_POST[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        return sanitize_email((string) wp_unslash($_POST[$key]));
    }

    public static function post_url(string $key, string $default = ''): string {
        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        if (!isset($_POST[$key])) {
            return $default;
        }

        // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified by form handlers before saves; this helper only sanitizes repopulation values.
        return esc_url_raw((string) wp_unslash($_POST[$key]));
    }
}
