<?php

namespace GriffinForms\Includes\ImportExport;

class FormThemeExport extends \GriffinForms\Includes\ImportExport\Format
{
    protected $theme_id;

    public function __construct($theme_id) {
        $this->theme_id = $theme_id;
        $this->setAll();
    }

    /**
     * Fetch the form theme data based on theme ID
     * Returns the theme in a JSON-serializable format suitable for export
     */
    public function getThemeData() {
        global $wpdb;
        $table = $this->config->getTable('formtheme');
        $theme = $wpdb->get_row($wpdb->prepare("SELECT * FROM %i WHERE id = %d", $table, $this->theme_id), ARRAY_A);

        if (!$theme) {
            return null;
        }

        // Decode any JSON-encoded fields
        $theme['form_style'] = !empty($theme['form_style']) ? $this->maybeDecode($theme['form_style']) : [];
        $theme['page_style'] = !empty($theme['page_style']) ? $this->maybeDecode($theme['page_style']) : [];
        $theme['row_style'] = !empty($theme['row_style']) ? $this->maybeDecode($theme['row_style']) : [];
        $theme['field_style'] = !empty($theme['field_style']) ? $this->maybeDecode($theme['field_style']) : [];
        $theme['button_typography'] = !empty($theme['button_typography']) ? $this->maybeDecode($theme['button_typography']) : [];
        $theme['submit_button'] = !empty($theme['submit_button']) ? $this->maybeDecode($theme['submit_button']) : [];
        $theme['previous_button'] = !empty($theme['previous_button']) ? $this->maybeDecode($theme['previous_button']) : [];
        $theme['next_button'] = !empty($theme['next_button']) ? $this->maybeDecode($theme['next_button']) : [];
        $theme['form_title'] = !empty($theme['form_title']) ? $this->maybeDecode($theme['form_title']) : [];
        $theme['form_description'] = !empty($theme['form_description']) ? $this->maybeDecode($theme['form_description']) : [];
        $theme['page_heading'] = !empty($theme['page_heading']) ? $this->maybeDecode($theme['page_heading']) : [];
        $theme['page_description'] = !empty($theme['page_description']) ? $this->maybeDecode($theme['page_description']) : [];
        $theme['row_heading'] = !empty($theme['row_heading']) ? $this->maybeDecode($theme['row_heading']) : [];
        $theme['row_description'] = !empty($theme['row_description']) ? $this->maybeDecode($theme['row_description']) : [];
        $theme['field_label'] = !empty($theme['field_label']) ? $this->maybeDecode($theme['field_label']) : [];
        $theme['field_description'] = !empty($theme['field_description']) ? $this->maybeDecode($theme['field_description']) : [];
        $theme['field_errors'] = !empty($theme['field_errors']) ? $this->maybeDecode($theme['field_errors']) : [];
        $theme['success_message'] = !empty($theme['success_message']) ? $this->maybeDecode($theme['success_message']) : [];

        // Verify JSON encoding
        $json_data = json_encode($theme);
        if (json_last_error() !== JSON_ERROR_NONE) {
            $this->log->add(
                'error',                              // log_type
                'formtheme',                          // item_type
                $this->theme_id,                      // item_id
                get_current_user_id(),                // user_id
                'JSON encoding error: ' . json_last_error_msg(), // message
                'json_encoding'                       // category
            );
            return null;
        }

        return $theme;
    }
}
