<?php

namespace Signteer\Core\WordPress;

class SheetPostType
{
    public const POST_TYPE = 'signteer_sheet';
    private static $defaultBaseSlug = 'signupsheet';

    public static function getBaseSlug()
    {
        return ($ret = get_option('signteer_sheet_slug')) ? $ret : self::$defaultBaseSlug;
    }

    public static function register()
    {
        $args = [
            'public' => true,
            'hierarchical' => false,
            'show_ui' => true,
            'show_in_menu' => false,
            'show_in_nav_menus'	=> false,
            'show_in_rest' => false,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => self::getBaseSlug(),
            'query_var' => true,
            // 'can_export' => true,
            'rewrite' => ['slug' => self::getBaseSlug()],
        ];

        if (!post_type_exists(self::POST_TYPE)) {
            register_post_type(self::POST_TYPE, $args);
        }

        if (get_transient('signteer_flush_rewrite_rules')) {
            flush_rewrite_rules();
            delete_transient('signteer_flush_rewrite_rules');
        }
    }

    public static function insert(string $title, string $description): int
    {
        $a = [
            'post_title' => $title,
            'post_content' => $description,
            'post_type' => self::POST_TYPE,
            'post_status' => 'publish',
        ];

        $id = wp_insert_post($a, true);

        return $id;
    }

    public static function updateTitle(int $id, string $title)
    {
        $a = ['ID' => $id, 'post_title' => $title];
        wp_update_post($a);
    }

    public static function updateDescription(int $id, string $description)
    {
        $a = ['ID' => $id, 'post_content' => $description];
        wp_update_post($a);
    }
}
