<?php
/**
 * Roles and capabilities.
 *
 * @package KGV\VereinManager
 */

namespace KGV\VereinManager;

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

class Roles {

	const MEMBER_ROLE  = 'kgv_member';
	const SETTINGS_CAP = 'manage_kleingarten_settings';

	/**
	 * Capability list.
	 *
	 * @return array
	 */
	public static function caps() {
		return array(
			'manage_kleingarten',
			'edit_sparten',
			'edit_parzellen',
			'edit_zaehler',
			'edit_paechter',
			'view_assigned_parcels',
			'submit_meter_readings',
		);
	}

	/**
	 * Resolve all role slugs that should count as "Vorstand".
	 *
	 * @return array
	 */
	private static function get_vorstand_roles() {
		$roles    = array( 'vorstand', 'kgv_vorstand' );
		$wp_roles = wp_roles();

		if ( $wp_roles instanceof \WP_Roles ) {
			foreach ( $wp_roles->roles as $role_slug => $role_data ) {
				$role_name = isset( $role_data['name'] ) ? sanitize_key( str_replace( ' ', '_', strtolower( (string) $role_data['name'] ) ) ) : '';

				if ( 'vorstand' === $role_name ) {
					$roles[] = $role_slug;
				}
			}
		}

		return array_values( array_unique( array_filter( $roles ) ) );
	}

	/**
	 * Create custom role and attach caps to admin/editor.
	 *
	 * @return void
	 */
	public static function add_roles_and_caps() {
		if ( ! get_role( self::MEMBER_ROLE ) ) {
			add_role(
				self::MEMBER_ROLE,
				__( 'Mitglied', KGVVM_TEXT_DOMAIN ),
				array(
					'read' => true,
				)
			);
		}

		$member_role = get_role( self::MEMBER_ROLE );

		if ( $member_role ) {
			$member_role->add_cap( 'read' );
			$member_role->add_cap( 'view_assigned_parcels' );
			$member_role->add_cap( 'submit_meter_readings' );
		}

		$management_roles = array_values( array_unique( array_merge( array( 'administrator', 'editor' ), self::get_vorstand_roles() ) ) );
		$settings_roles   = array_values( array_unique( array_merge( array( 'administrator' ), self::get_vorstand_roles() ) ) );

		foreach ( $management_roles as $role_name ) {
			$role = get_role( $role_name );

			if ( ! $role ) {
				continue;
			}

			foreach ( self::caps() as $cap ) {
				$role->add_cap( $cap );
			}
		}

		foreach ( $settings_roles as $role_name ) {
			$role = get_role( $role_name );

			if ( $role ) {
				$role->add_cap( self::SETTINGS_CAP );
			}
		}
	}

	/**
	 * Remove custom role and capabilities.
	 *
	 * @return void
	 */
	public static function remove_roles_and_caps() {
		remove_role( self::MEMBER_ROLE );

		$management_roles = array_values( array_unique( array_merge( array( 'administrator', 'editor' ), self::get_vorstand_roles() ) ) );
		$settings_roles   = array_values( array_unique( array_merge( array( 'administrator' ), self::get_vorstand_roles() ) ) );

		foreach ( $management_roles as $role_name ) {
			$role = get_role( $role_name );

			if ( ! $role ) {
				continue;
			}

			foreach ( self::caps() as $cap ) {
				$role->remove_cap( $cap );
			}
		}

		foreach ( $settings_roles as $role_name ) {
			$role = get_role( $role_name );

			if ( $role ) {
				$role->remove_cap( self::SETTINGS_CAP );
			}
		}
	}
}
