<?php
declare(strict_types=1);
namespace ConfManager;

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


/**
 * カスタムロール・ケイパビリティの定義と管理
 *
 * @since 1.8.0
 */
class Capabilities {

	/**
	 * 全カスタムケイパビリティ一覧
	 */
	public const ALL_CAPS = [
		'conf_manager_access',
		'conf_manager_manage_settings',
		'conf_manager_view_dashboard',
		'conf_manager_view_events',
		'conf_manager_edit_events',
		'conf_manager_view_members',
		'conf_manager_edit_members',
		'conf_manager_import_members',
		'conf_manager_export_members',
		'conf_manager_view_registrations',
		'conf_manager_edit_registrations',
		'conf_manager_checkin',
		'conf_manager_view_payments',
		'conf_manager_edit_payments',
		'conf_manager_export_payments',
		'conf_manager_view_notifications',
		'conf_manager_edit_notifications',
		'conf_manager_view_sponsors',
		'conf_manager_edit_sponsors',
		'conf_manager_view_submissions',
		'conf_manager_edit_submissions',
		'conf_manager_view_promotions',
		'conf_manager_edit_promotions',
		// Review system capabilities (v2.0)
		'conf_manager_review_submissions',
		'conf_manager_assign_reviewers',
		'conf_manager_screen_submissions',
		'conf_manager_make_decisions',
	];

	/**
	 * カスタムロール定義
	 *
	 * @return array<string, array{display_name: string, caps: string[]}>
	 */
	public static function get_roles(): array {
		return [
			'conf_editor' => [
				'display_name' => __( 'Conference Editor', 'conferencemanager' ),
				'caps' => [
					'conf_manager_access',
					'conf_manager_view_dashboard',
					'conf_manager_view_events',
					'conf_manager_edit_events',
					'conf_manager_view_members',
					'conf_manager_edit_members',
					'conf_manager_import_members',
					'conf_manager_export_members',
					'conf_manager_view_registrations',
					'conf_manager_edit_registrations',
					'conf_manager_checkin',
					'conf_manager_view_payments',
					'conf_manager_edit_payments',
					'conf_manager_export_payments',
					'conf_manager_view_notifications',
					'conf_manager_edit_notifications',
					'conf_manager_view_sponsors',
					'conf_manager_edit_sponsors',
					'conf_manager_view_submissions',
					'conf_manager_edit_submissions',
					'conf_manager_view_promotions',
					'conf_manager_edit_promotions',
					'conf_manager_assign_reviewers',
				],
			],
			'conf_editor_in_chief' => [
				'display_name' => __( 'Editor-in-Chief', 'conferencemanager' ),
				'caps' => [
					'conf_manager_access',
					'conf_manager_view_dashboard',
					'conf_manager_view_events',
					'conf_manager_view_members',
					'conf_manager_view_submissions',
					'conf_manager_edit_submissions',
					'conf_manager_assign_reviewers',
					'conf_manager_screen_submissions',
					'conf_manager_make_decisions',
				],
			],
			'conf_associate_editor' => [
				'display_name' => __( 'Associate Editor', 'conferencemanager' ),
				'caps' => [
					'conf_manager_access',
					'conf_manager_view_dashboard',
					'conf_manager_view_events',
					'conf_manager_view_members',
					'conf_manager_view_submissions',
					'conf_manager_assign_reviewers',
				],
			],
			'conf_reviewer' => [
				'display_name' => __( 'Reviewer', 'conferencemanager' ),
				'caps' => [
					'conf_manager_access',
					'conf_manager_review_submissions',
				],
			],
			'conf_viewer' => [
				'display_name' => __( 'Conference Viewer', 'conferencemanager' ),
				'caps' => [
					'conf_manager_access',
					'conf_manager_view_dashboard',
					'conf_manager_view_events',
					'conf_manager_view_registrations',
					'conf_manager_view_payments',
					'conf_manager_view_sponsors',
					'conf_manager_view_submissions',
				],
			],
			'conf_staff' => [
				'display_name' => __( 'Conference Staff', 'conferencemanager' ),
				'caps' => [
					'conf_manager_access',
					'conf_manager_view_events',
					'conf_manager_checkin',
				],
			],
		];
	}

	/**
	 * カスタムロールを登録し、administratorに全capを追加
	 */
	public static function register_roles(): void {
		// カスタムロールの作成
		foreach ( self::get_roles() as $slug => $role ) {
			$caps = [ 'read' => true ];
			foreach ( $role['caps'] as $cap ) {
				$caps[ $cap ] = true;
			}
			// 既存ロールがあれば一旦削除して再作成（cap更新のため）
			if ( get_role( $slug ) ) {
				remove_role( $slug );
			}
			add_role( $slug, $role['display_name'], $caps );
		}

		// administrator に全カスタムcapを追加
		$admin = get_role( 'administrator' );
		if ( $admin ) {
			foreach ( self::ALL_CAPS as $cap ) {
				$admin->add_cap( $cap );
			}
		}
	}

	/**
	 * カスタムロールを削除し、administratorからカスタムcapを除去
	 */
	public static function unregister_roles(): void {
		foreach ( array_keys( self::get_roles() ) as $slug ) {
			remove_role( $slug );
		}

		$admin = get_role( 'administrator' );
		if ( $admin ) {
			foreach ( self::ALL_CAPS as $cap ) {
				$admin->remove_cap( $cap );
			}
		}
	}

	/**
	 * 現在のユーザーが持つプラグイン関連capのマップを返す
	 *
	 * @return array<string, bool>
	 */
	public static function get_current_user_caps(): array {
		$result = [];
		foreach ( self::ALL_CAPS as $cap ) {
			$result[ $cap ] = current_user_can( $cap );
		}
		return $result;
	}
}
