<?php
/**
 * Author: Rymera Web Co.
 *
 * @package SAAI\Classes
 */

namespace SAAI\Classes;

/**
 * Category_Description class.
 *
 * @since 1.0.4
 */
class Category_Description {

    const META_KEY = '_saai_category_description_form_data';

    /**
     * The category ID.
     *
     * @var int
     */
    private $term_id;

    /**
     * The category term.
     *
     * @var \WP_Term
     */
    private $term;

    /**
     * The data.
     *
     * @var array
     */
    private $data = array();

    /**
     * Constructor.
     *
     * @param int $term_id The category term ID.
     */
    public function __construct( $term_id = 0 ) {
        $this->term_id = absint( $term_id );
        $this->load_data();
    }

    /**
     * Load the data.
     *
     * @since 1.0.4
     */
    public function load_data() {
        $term = $this->get_term();

        if ( $term instanceof \WP_Term ) {
            $this->data = wp_parse_args( get_term_meta( $this->term_id, self::META_KEY, true ), self::get_default_data() );

            $category_name = $term->name;

            // If the category name is not set, set it to the term name.
            if ( ! isset( $this->data['category_name'] ) || empty( $this->data['category_name'] ) ) {
                $this->data['category_name'] = $category_name;
            }
        } else {
            $this->data = self::get_default_data();
        }
    }

    /**
     * Get the term.
     *
     * @since 1.0.4
     *
     * @return \WP_Term
     */
    public function get_term() {
        if ( ! $this->term ) {
            $temp_term = get_term( $this->term_id, 'product_cat' );

            if ( ! $temp_term || is_wp_error( $temp_term ) ) {
                return new \WP_Error( 'term_not_found', 'Category not found', array( 'status' => 404 ) );
            }

            $this->term = $temp_term;
        }

        return $this->term;
    }

    /**
     * Get the default data.
     *
     * @since 1.0.4
     *
     * @return array
     */
    public static function get_default_data() {
        // Get default values from the AI agent settings.
        $ai_agent_settings       = array();
        $category_description_ai = \SAAI\AI_Agents\Category_Description_AI::instance();

        if ( $category_description_ai ) {
            $ai_agent_settings = $category_description_ai->get_settings();
        }

        $defaults = array(
            'need_category_name' => '',
            'category_name'      => '',
            'tones'              => $ai_agent_settings['tones'] ?? array(),
            'characteristics'    => $ai_agent_settings['characteristics'] ?? array(),
            'age_focus'          => $ai_agent_settings['age_focus'] ?? array(),
            'target_audiences'   => $ai_agent_settings['target_audience'] ?? array(), // Note the key difference.
            'keywords'           => array(),
            'description_length' => $ai_agent_settings['description_length'] ?? '',
            'language'           => $ai_agent_settings['language'] ?? 'default',
        );

        return $defaults;
    }

    /**
     * Get the data.
     *
     * @since 1.0.4
     *
     * @return array
     */
    public function get_data() {
        if ( ! is_array( $this->data ) || empty( $this->data ) ) {
            return self::get_default_data();
        }

        return $this->data;
    }

    /**
     * Set the data.
     *
     * @since 1.0.4
     *
     * @param array $data The data.
     */
    public function set_data( $data ) {
        $existing_data = $this->get_data();
        $this->data    = wp_parse_args( $data, $existing_data );
    }

    /**
     * Save the data.
     *
     * @since 1.0.4
     *
     * @return bool|\WP_Error
     */
    public function save() {
        $term = $this->get_term();

        if ( is_wp_error( $term ) ) {
            return $term;
        }

        return update_term_meta( $this->term_id, self::META_KEY, $this->data );
    }

    /**
     * Sanitize the data.
     *
     * @since 1.0.4
     *
     * @param array $data The data.
     * @return array
     */
    public static function sanitize_data( $data ) {
        $sanitized = array();

        foreach ( $data as $key => $value ) {
            switch ( $key ) {
                case 'tones':
                case 'characteristics':
                case 'age_focus':
                case 'target_audiences':
                case 'keywords':
                    $sanitized[ $key ] = array_map( 'sanitize_text_field', $value );
                    break;
                default:
                    $sanitized[ $key ] = sanitize_text_field( $value );
            }
        }

        return $sanitized;
    }
}
