<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Controller\Api;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Simplyin\Payment\Helper\ApiClient;
use Psr\Log\LoggerInterface;

/**
 * Resend OTP controller
 *
 * Handles resending OTP via SMS or email
 */
class ResendOtp implements HttpPostActionInterface, CsrfAwareActionInterface
{
    use CsrfAwareApiTrait;

    /**
     * @var JsonFactory
     */
    private $resultJsonFactory;

    /**
     * @var ApiClient
     */
    private $apiClient;

    /**
     * @var RequestInterface
     */
    private $request;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * Constructor
     *
     * @param JsonFactory $resultJsonFactory
     * @param ApiClient $apiClient
     * @param RequestInterface $request
     * @param LoggerInterface $logger
     */
    public function __construct(
        JsonFactory $resultJsonFactory,
        ApiClient $apiClient,
        RequestInterface $request,
        LoggerInterface $logger
    ) {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->apiClient = $apiClient;
        $this->request = $request;
        $this->logger = $logger;
    }

    /**
     * Execute resend OTP action
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $result = $this->resultJsonFactory->create();
        
        try {
            $data = json_decode($this->request->getContent(), true);
            $email = $data['email'] ?? '';
            $method = $data['method'] ?? 'sms'; // 'sms' or 'email'

            if (empty($email)) {
                return $result->setData([
                    'success' => false,
                    'message' => __('Email is required')
                ]);
            }

            // Call appropriate API method based on delivery method
            if ($method === 'email') {
                $response = $this->apiClient->resendOtpViaEmail($email);
                $deliveryMethod = 'email';
            } else {
                $response = $this->apiClient->resendOtpViaSms($email);
                $deliveryMethod = 'SMS';
            }

            if ($response['success']) {
                return $result->setData([
                    'success' => true,
                    'message' => __('OTP resent to your %1', $deliveryMethod),
                    'method' => $method,
                    'data' => $response['data'] ?? []
                ]);
            } else {
                return $result->setData([
                    'success' => false,
                    'message' => $response['error'] ?? __('Failed to resend OTP')
                ]);
            }
        } catch (\Exception $e) {
            $this->logger->error('SimplyIN ResendOtp: ' . $e->getMessage(), [
                'trace' => $e->getTraceAsString()
            ]);
            return $result->setData([
                'success' => false,
                'message' => __('An unexpected error occurred. Please try again.')
            ]);
        }
    }
}
