<?php

namespace GemBoards;

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

use Exception;
use GemBoards\Settings\Base;
use GemBoards\Utils\Helper;

class Installer {

	public static function init() {
		add_action( 'init', [ __CLASS__, 'check_version' ], 5 );
	}

	/**
	 * Check version & run installer if needed.
	 *
	 * @return void
	 * @since 0.0.4
	 */
	public static function check_version() {
		$gemboards_version = get_option( 'gemboards_version' );
		$requires_update   = version_compare( $gemboards_version, GEMBOARDS_VERSION, '<' );
		if ( ! defined( 'IFRAME_REQUEST' ) && $requires_update ) {
			define( 'GEMBOARDS_UPDATING', true );
			// Run the installer.
			self::install();
			Migration::run_migration($gemboards_version);

			/**
			 * Run after Gemboards has been updated.
			 *
			 * @since 0.0.4
			 */
			do_action( 'gemboards/updated' );
		}
	}

	public static function install() {
		if ( ! is_blog_installed() ) {
			return;
		}

		// Check if we are not already running this routine.
		if ( 'yes' === get_transient( 'gemboards_installing' ) ) {
			return;
		}

		// If we made it till here nothing is running yet, lets set the transient now.
		set_transient( 'gemboards_installing', 'yes', MINUTE_IN_SECONDS * 10 );

		define( 'GEMBOARDS_INSTALLING', true );

		( new RewriteRules() )->add_rewrite_rules();
		flush_rewrite_rules();

		global $wpdb;
		$wpdb->hide_errors();

		try {
			$self = new self();
			Database::create_initial_custom_table();
			$self->add_role();
			$self->add_settings();

			update_option( 'gemboards_version', GEMBOARDS_VERSION );
		} catch ( Exception $e ) {
			Helper::error_log( $e->getMessage() );
		} finally {
			delete_transient( 'gemboards_installing' );
		}

		/**
		 * Run after GemBoards has been installed or updated.
		 *
		 * @since 0.0.4
		 */
		do_action( 'gemboards/installed' );
	}

	public function add_role() {
		$this->update_administrator_role();
		$this->add_admin_role();
	}

	private function update_administrator_role() {
		$capabilities = [
			'edit_posts',
			'edit_others_posts',
			'edit_gemboards_task',
			'read_gemboards_task',
			'delete_gemboards_task',
			'read_private_gemboards_tasks',
			'edit_gemboards_tasks',
			'publish_gemboards_tasks',
			'delete_gemboards_tasks',
			'edit_others_gemboards_tasks',
			'manage_gemboards',
		];

		$administrator = get_role( 'administrator' );
		if ( $administrator ) {
			foreach ( $capabilities as $capability ) {
				$administrator->add_cap( $capability );
			}
		}
	}

	private function add_admin_role() {
		remove_role( 'gemboards_admin' );

		add_role( 'gemboards_admin', esc_html__( 'GemBoards Admin', 'gemboards' ), [
			'edit_gemboards_task',
			'read_gemboards_task',
			'delete_gemboards_task',
			'read_private_gemboards_tasks',
			'edit_gemboards_tasks',
			'publish_gemboards_tasks',
			'edit_post',
			'edit_post_meta',
			'assign_terms',
			'assign_term',
			'read',
			'upload_files',
			'edit_posts',
			'manage_gemboards',
			'list_users',
			'promote_users',
		] );
	}

	public function add_settings() {
		$settings = get_option( GEMBOARDS_SETTINGS_NAME );
		if ( ! $settings ) {
			Base::save_settings();
		}
	}
}
