<?php
/**
 * @author 1902 Software
 * @copyright Copyright © 2023 1902 Software (https://1902software.com/magento/)
 * @package WriteTextAI_WriteTextAI
 */

declare(strict_types=1);

namespace WriteTextAI\WriteTextAI\Controller\Adminhtml\Settings;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use WriteTextAI\WriteTextAI\Model\UserFieldsSelectedManager;

class Save extends Action
{
    /**
     * @var UserFieldsSelectedManager
     */
    protected $userFieldsSelectedManager;

    /**
     * Constructor
     *
     * @param Context $context
     * @param UserFieldsSelectedManager $userFieldsSelectedManager
     */
    public function __construct(
        Context $context,
        UserFieldsSelectedManager $userFieldsSelectedManager
    ) {
        $this->userFieldsSelectedManager = $userFieldsSelectedManager;
        parent::__construct($context);
    }

    /**
     * Execute
     *
     * @return \Magento\Framework\Controller\Result\Json
     */
    public function execute()
    {
        $response = $this->resultFactory->create(ResultFactory::TYPE_JSON);

        try {
            $params = $this->getRequest()->getParams();
            $entityType = $params['entity_type'] ?? 'product';
            $scope = $params['scope'] ?? '';

            $mirrorMap = [
                UserFieldsSelectedManager::ACTION_TYPE_BULK_GENERATE_FIELDS
                    => UserFieldsSelectedManager::ACTION_TYPE_BULK_FULL_AUTOMATION_FIELDS,
                UserFieldsSelectedManager::ACTION_TYPE_BULK_FULL_AUTOMATION_FIELDS
                    => UserFieldsSelectedManager::ACTION_TYPE_BULK_GENERATE_FIELDS
            ];

            foreach ($params as $param => $value) {
                if (!in_array(
                    $param,
                    array_values((new \ReflectionClass(UserFieldsSelectedManager::class))->getConstants())
                )) {
                    continue;
                }
                $this->userFieldsSelectedManager->saveUserFieldsSelected(
                    $value,
                    $param,
                    $entityType,
                    $scope
                );

                if (isset($mirrorMap[$param]) && !array_key_exists($mirrorMap[$param], $params)) {
                    $this->userFieldsSelectedManager->saveUserFieldsSelected(
                        $value,
                        $mirrorMap[$param],
                        $entityType,
                        $scope
                    );
                }
            }
            
            $result = [
                'success' => true
            ];
        } catch (\Exception $e) {
            $message = $e->getMessage();
            if (!$message) {
                $message = __(
                    'A system error has occurred. Please try again. If the issue persists,'
                    . ' please contact our support team at support@writetext.ai.'
                );
            }
            
            $result = [
                'success' => false,
                'message' => $message
            ];
        }

        $response->setData($result);

        return $response;
    }
}
