<?php
namespace ThemeAtelier\DomainForSale\Includes;

use ThemeAtelier\DomainForSale\Admin\Helpers\Helpers;

/**
 * Fired during plugin activation
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package    Domain_For_Sale
 * @subpackage src/Domain_For_Sale
 */

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Domain_For_Sale
 * @subpackage src/Domain_For_Sale
 * @author     ThemeAtelier <themeatelierbd@gmail.com>
 */
class Domain_For_Sale_Activator {

	/**
	 * Create post on plugin activation
	 *
	 * This function will create a default domain for post layout if we don't have any.
	 *
	 * @since    1.0.0
	 */
	public static function activate() {
        $existing_posts = new \WP_Query(array(
            'post_type' => 'dfs_template',
            'posts_per_page' => 1,
        ));

        // If no posts exist, create the default post
        if (!$existing_posts->have_posts()) {
            $post_data = array(
                'post_title'    => esc_html__('Layout', 'domain-for-sale'),
                'post_status'   => 'publish',
                'post_type'     => 'dfs_template',

            );
            $post_id = wp_insert_post($post_data);
            if (! is_wp_error($post_id)) {
                // Meta values to insert under 'dfs_template_options'
                $template_options = Helpers::dfs_default_domain_meta();

                $layout_template = array(
                    'dfs_layout_preset' => 'layout_01',
                );

                // Save all the meta values under the 'dfs_template_options' meta key
                update_post_meta($post_id, 'dfs_layouts', $layout_template);
                update_post_meta($post_id, 'dfs_template_options', $template_options);
            }
        }
	}

}
