<?php
namespace OxygenApp\Models;

if ( ! defined( 'ABSPATH' ) ) exit;

use OxygenApp\Utils\LanguagesHelper;

class BackgroundCategory {
    const TAX = 'ox_background_category';
    const META_KEY = 'ox_background_category_meta';
    

    static function getAll() {
        return get_terms(['taxonomy' => self::TAX, 'hide_empty' => FALSE, 'parent' => 0]);
    }

    static function getAllPretty(): ?array {
        $all = self::getAll();
        $out = [];
        foreach ($all as $key => $cat) {
            $out[] = self::getPretty($cat);
        }
        return $out;
    }

    static function getPretty(\WP_Term $term): ?array {
        if (!$term) {
            return null;
        }

        $labels = self::getTermLabels($term->term_id);
        $pretty = [
            'term_id' => $term->term_id,
            'name' => $term->name,
            'slug' => $term->slug,
            'bkg_cat_name' => esc_html($labels['bkg_cat_name'] ?? ''),
        ];

        return $pretty;
    }

    static function onSaveMeta($term_id) {

        // Verify nonce
        if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'update-tag_' . $term_id)) {
            return;
        }

        // Check user capability
        if (!current_user_can('edit_term', $term_id)) {
            wp_die(esc_html__('You do not have permission to edit this term.', 'oxyleaf-app-working-and-relaxing-app'));
        }

        $isPlaylist = isset($_POST['taxonomy']) && sanitize_text_field(wp_unslash($_POST['taxonomy'])) === BackgroundCategory::TAX;
        if (!$isPlaylist) {
            return;
        }

        $labelsData = isset($_POST['labels_' . self::META_KEY]) ? map_deep(wp_unslash($_POST['labels_' . self::META_KEY]), 'sanitize_text_field') : [];
        $sanitizedLabels = [];


        foreach ($labelsData as $langKey => $labels) {
            $lngKey = sanitize_text_field(wp_unslash($langKey));
            $sanitizedLabels[$lngKey] = [];
            foreach ($labels as $labelKey => $label) {
                $sanitizedLabels[$lngKey][$labelKey] = sanitize_text_field(wp_unslash($label));
            }
        }

        update_term_meta(
            $term_id,
            'labels_' . self::META_KEY,
            $sanitizedLabels
        );
    }

    static function getTermLabels(int $term_id): ?array {
        $currentLangCode = LanguagesHelper::getInstance()->getCurrentLanguageCode();
        $labelsValue = get_term_meta($term_id, "labels_" . self::META_KEY, true);

        return $labelsValue[$currentLangCode] ?? [];
    }

    public static function cleanupOnUninstall(): void {
        $terms = get_terms([
            'taxonomy' => self::TAX,
            'hide_empty' => false,
            'fields' => 'ids',
        ]);

        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term_id) {
                delete_term_meta($term_id, 'labels_' . self::META_KEY);
                wp_delete_term($term_id, self::TAX);
            }
        }
    }
}