<?php

namespace WKO\API\Routes;

use WKO\Controllers\Render\CreditCheckout;

class Installments extends Route
{
    public function __construct()
    {
        $this->setNamespace();
        $this->registerRoute(
            'installments',
            [$this, 'handleRequest'],
            ['POST']
        );
    }

    public function handleRequest($data)
    {
        try {
            $nonce = sanitize_text_field(wp_unslash($data->get_param('_wpnonce')));

            if (!wp_verify_nonce($nonce, 'wp_rest')) {
                $this->sendJsonResponse(
                    __('Invalid Nonce!', 'wc-koin-official'),
                    false,
                    403
                );
                return;
            }

            $params = [
                'brand' => sanitize_text_field(wp_unslash($data->get_param('brand'))),
                'number' => str_replace(' ', '', sanitize_text_field(wp_unslash($data->get_param('number')))),
                'total' => floatval($data->get_param('total'))
            ];


            $this->validateFields($params);

            $checkout = new CreditCheckout();
            $installments = $checkout->get_installments($params['total'], $params['number'], $params['brand']);

            $this->sendJsonResponse(
                '',
                true,
                200,
                $installments
            );
        } catch (\Throwable $th) {
            $this->sendJsonResponse(
                __('An error occurred while processing your request.', 'wc-koin-official'),
                false,
                500
            );
        }
    }

    private function validateFields(array $data): void
    {
        $fields = [
            'brand' => 'string',
            'number' => 'string',
            'total' => 'numeric'
        ];

        foreach ($fields as $field => $type) {
            if (!isset($data[$field])) {
                $this->sendJsonResponse(
                    sprintf(
                        __('The parameter "%s" is required!', 'wc-koin-official'),
                        $field
                    ),
                    false,
                    422
                );
                return;
            }

            $value = $data[$field];

            if ($type === 'string' && !is_string($value)) {
                $this->sendJsonResponse(
                    sprintf(
                        __('The parameter "%s" must be a string!', 'wc-koin-official'),
                        $field
                    ),
                    false,
                    422
                );
                return;
            }

            if ($type === 'numeric' && !is_numeric($value)) {
                $this->sendJsonResponse(
                    sprintf(
                        __('The parameter "%s" must be numeric!', 'wc-koin-official'),
                        $field
                    ),
                    false,
                    422
                );
                return;
            }
        }
    }
}
