<?php

namespace LoftyIDX\includes;

use LoftyIDX\includes\common\LoftyIDXApiCache;

defined('ABSPATH') || exit;

class LoftyIDXInstall
{
  public static function install()
  {
    if (!is_blog_installed()) {
      return;
    }
    LoftyIDXApiCache::clearAllCache();
    self::create_options();
    
    /**
     * Set flag to flush the rewrite rules on next page load after install or update;
     */
    update_option('lofty_idx_needs_rewrite_flush', true);

    /**
     * Run after the plugin is installed or updated.
     */
    do_action('lofty_idx_installed');

    /**
     * Run after the admin is installed or updated;
     */
    do_action('lofty_idx_admin_installed');

  }

  public static function uninstall()
  {
    // Set flag to flush rewrite rules on next page load
    update_option('lofty_idx_needs_rewrite_flush', true);
    LoftyIDXApiCache::clearAllCache();
  }

  /**
   * Create default options and store into the database;
   */
  public static function create_options()
  {
    add_option('lofty_idx_secret_key', '', '', false);
    add_option('lofty_idx_listing_page_id', '', '', false);
    add_option('lofty_idx_listing_detail_page_id', '', '', false);
    add_option('lofty_idx_sold_listing_page_id', '', '', false);
    add_option('lofty_idx_sold_detail_page_id', '', '', false);

    add_option('lofty_idx_db_version', LOFTY_IDX_VERSION, '', false);

    // global customization options
    add_option('lofty_idx_custom_css', '', '');
    add_option('lofty_idx_custom_js', '', '');
    add_option('lofty_idx_custom_html', '', '');
    
    // Clear any pending rewrite flush flag for clean installation
    delete_option('lofty_idx_needs_rewrite_flush');
  }

}
