<?php
namespace OxygenApp\Models;

use WP_Post;

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

class Background {

    const POST_TYPE = 'oxygen_bks';
    const POST_TYPE_SINGULAR = 'oxygen_bk';
    const POST_META_KEY = 'oxygen_bk_meta';

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

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

    static function getById($id): ?\WP_Post {
        $id = absint($id);
        if ($id <= 0) {
            return null;
        }
        $post = get_post($id);
        if (!$post) {
            return null;
        }
        return $post;
    }

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

        $type = get_post_meta($post->ID, 'type_' . self::POST_META_KEY, true);

        $background_credits_label = get_post_meta($post->ID, 'background_credits_label_' . self::POST_META_KEY, true);
        $background_credits_url = get_post_meta($post->ID, 'background_credits_url_' . self::POST_META_KEY, true);

        // Get background category
        $categoryId = null;
        $terms = wp_get_post_terms($post->ID, BackgroundCategory::TAX);
        if (!is_wp_error($terms) && !empty($terms)) {
            $categoryId = $terms[0]->term_id;
        }

        $out = [
            'id' => $post->ID,
            'title' => $post->post_title,
            'type' => $type,
            'category_id' => $categoryId,
            'background_credits_label' => esc_html($background_credits_label),
            'background_credits_url' => esc_url($background_credits_url),
        ];

        if ($type === 'video') {
            $videoFileId = get_post_meta($post->ID, 'video_file_id_' . self::POST_META_KEY, true);
            if ($shouldIncludePremiumUrl && $videoFileId && is_numeric($videoFileId)) {
                $video_attachment_url = wp_get_attachment_url($videoFileId);
                if ($video_attachment_url) {
                    $out['video_url'] = $video_attachment_url;
                }
            }
            $videoIconId = get_post_meta($post->ID, 'video_icon_file_id_' . self::POST_META_KEY, true);
            if ($videoIconId && is_numeric($videoIconId)) {
                $videoIconUrl = wp_get_attachment_image_url($videoIconId, 'medium');
                $out['video_icon'] = $videoIconUrl;
            }
        } else if ($type === 'image') {
            $bkImageId = get_post_meta($post->ID, 'bk_image_file_id_' . Background::POST_META_KEY, true);

            if ($shouldIncludePremiumUrl && $bkImageId && is_numeric($bkImageId)) {
                $image_attachment_url = wp_get_attachment_image_url($bkImageId, 'full');
                if ($image_attachment_url) {
                    $out['image_url_full'] = $image_attachment_url;
                }
            }

            if ($bkImageId && is_numeric($bkImageId)) {
                $image_attachment_url_medium = wp_get_attachment_image_url($bkImageId, 'medium');
                if ($image_attachment_url_medium) {
                    $out['image_url_medium'] = $image_attachment_url_medium;
                }
            }

        }

        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, 'type_' . self::POST_META_KEY);
            delete_post_meta($post->ID, 'video_file_id_' . self::POST_META_KEY);
            delete_post_meta($post->ID, 'video_icon_file_id_' . self::POST_META_KEY);
            delete_post_meta($post->ID, 'bk_image_file_id_' . self::POST_META_KEY);
            wp_delete_post($post->ID, true);
        }
    }
}