<?php
/**
 * Lantern Span People Directory activation and upgrade tasks.
 *
 * @package LanternSpanPeopleDirectory
 */

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

/**
 * Prepares the plugin for first use.
 */
class LSPD_Activator {

	/**
	 * Activates the plugin.
	 *
	 * @return void
	 */
	public static function activate() {
		LSPD_Post_Type::register_post_type();
		self::ensure_capabilities();
		self::set_default_settings();
		self::maybe_create_directory_page();

		update_option( 'lspd_version', LSPD_VERSION );
		set_transient( 'lspd_activation_notice', 1, MINUTE_IN_SECONDS * 10 );
	}

	/**
	 * Repairs installation tasks after a manual file update.
	 *
	 * @return void
	 */
	public static function maybe_upgrade() {
		if ( LSPD_VERSION === get_option( 'lspd_version' ) ) {
			return;
		}

		self::ensure_capabilities();
		self::set_default_settings();
		self::maybe_create_directory_page();
		update_option( 'lspd_version', LSPD_VERSION );
	}

	/**
	 * Adds directory-management capabilities to administrators.
	 *
	 * @return void
	 */
	public static function ensure_capabilities() {
		$role = get_role( 'administrator' );

		if ( ! $role ) {
			return;
		}

		foreach ( self::get_capabilities() as $capability ) {
			$role->add_cap( $capability );
		}
	}

	/**
	 * Returns all custom capabilities used by the profile post type.
	 *
	 * @return array<int, string>
	 */
	public static function get_capabilities() {
		return array(
			'edit_lspd_person',
			'read_lspd_person',
			'delete_lspd_person',
			'edit_lspd_people',
			'edit_others_lspd_people',
			'publish_lspd_people',
			'read_private_lspd_people',
			'delete_lspd_people',
			'delete_private_lspd_people',
			'delete_published_lspd_people',
			'delete_others_lspd_people',
			'edit_private_lspd_people',
			'edit_published_lspd_people',
			'create_lspd_people',
		);
	}

	/**
	 * Creates default settings only when no migrated option exists.
	 *
	 * @return void
	 */
	private static function set_default_settings() {
		if ( is_array( get_option( 'lspd_settings', false ) ) ) {
			return;
		}

		add_option(
			'lspd_settings',
			array(
				'directory_page_id' => 0,
				'access'            => 'logged_in',
				'per_page'          => 24,
				'sort_by'           => 'last_name',
				'show_alphabet'     => 1,
			)
		);
	}

	/**
	 * Creates the initial Directory page when one is not configured.
	 *
	 * @return void
	 */
	private static function maybe_create_directory_page() {
		$settings = lspd_get_settings();
		$page_id  = absint( $settings['directory_page_id'] );

		if ( $page_id > 0 && get_post( $page_id ) ) {
			return;
		}

		$page_id = wp_insert_post(
			array(
				'post_title'   => __( 'Directory', 'lantern-span-people-directory' ),
				'post_name'    => 'directory',
				'post_content' => '[lantern_span_people_directory]',
				'post_status'  => 'publish',
				'post_type'    => 'page',
			),
			true
		);

		if ( is_wp_error( $page_id ) ) {
			return;
		}

		$settings['directory_page_id'] = absint( $page_id );
		lspd_update_settings( $settings );
	}
}
