<?php
namespace OxygenApp\Models;

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

use OxygenApp\Models\AdminPlaylist;
use OxygenApp\Utils\LanguagesHelper;

class BackgroundSound {

    const POST_TYPE = 'oxygen_app_bk_sounds';
    const POST_TYPE_SINGULAR = 'oxygen_app_bk_sound';
    const POST_META_KEY = 'oxygen_app_bk_sound_meta';
    const POST_SAVE_TRANSIENT_KEY = 'bk_sound_transient_key';

    public function getAll(array $filters = []): array {
        $args = [
            'post_type' => self::POST_TYPE,
            'posts_per_page' => -1,
            'post_status' => 'publish',
        ];

        $termId = $filters['term_id'] ?? null;
        if ($termId) {
            $args['tax_query'] = [
                [
                    'taxonomy' => AdminPlaylist::TAX,
                    'field' => 'term_id',
                    'terms' => $termId,
                ]
            ];
        }

        $posts = get_posts($args);
        return $posts;
    }

    static function getPretty(\WP_Post $post): ?array {
        if (!$post) {
            return null;
        }
        if ($post->post_type !== self::POST_TYPE) {
            return null;
        }
        if ($post->post_status !== 'publish') {
            return null;
        }

        $soundVolume = get_post_meta($post->ID, 'sound_volume_' . self::POST_META_KEY, true);
        $soundVolume = is_numeric($soundVolume) ? absint($soundVolume) : 50;

        $soundIconId = get_post_meta($post->ID, 'icon_file_id_' . self::POST_META_KEY, true);
        $soundIconUrl = '';
        if ($soundIconId) {
            $soundIconUrl = wp_get_attachment_image_url($soundIconId, 'full');
        }
        if ($soundIconId && is_numeric($soundIconId)) {
            $soundIconUrl = wp_get_attachment_url($soundIconId);
        }

        $labels = get_post_meta($post->ID, 'labels_' . self::POST_META_KEY, true);
        $soundName = $labels[LanguagesHelper::getInstance()->getCurrentLanguageCode()]['sound_name'] ?? $post->post_title;

        $sound_file_id_ = get_post_meta($post->ID, 'sound_file_id_' . self::POST_META_KEY, true);
        $soundFileUrl = '';
        if ($sound_file_id_ && is_numeric($sound_file_id_)) {
            $soundFileUrl = wp_get_attachment_url($sound_file_id_);
        }
        

        $out = [
            'id' => $post->ID,
            'title' => $post->post_title,
            'soundName' => esc_html($soundName),
            'soundIconUrl' => esc_url($soundIconUrl),
            'soundVolume' => $soundVolume,
            'soundFileUrl' => esc_url($soundFileUrl),
        ];

        return $out;
    }

    public static function cleanupOnUninstall(): void {
        $posts = get_posts([
            'post_type' => self::POST_TYPE,
            'posts_per_page' => -1,
            'post_status' => 'any',
        ]);

        foreach ($posts as $post) {
            delete_post_meta($post->ID, 'sound_volume_' . self::POST_META_KEY);
            delete_post_meta($post->ID, 'icon_file_id_' . self::POST_META_KEY);
            delete_post_meta($post->ID, 'labels_' . self::POST_META_KEY);
            delete_post_meta($post->ID, 'sound_file_id_' . self::POST_META_KEY);
            wp_delete_post($post->ID, true);
        }

        // Delete any transients
        delete_transient(self::POST_SAVE_TRANSIENT_KEY);
    }
}