<?php
/**
 * Role Manager
 *
 * Manages custom roles and capabilities.
 *
 * @package DonorCore\Security
 */

namespace DonorCore\Security;

defined( 'ABSPATH' ) || exit;

/**
 * Role Manager Class
 */
class Role_Manager {

	/**
	 * Create custom roles.
	 */
	public static function create_roles() {
		// Create Donation Manager role.
		add_role(
			'donorcore_donation_manager',
			'Donation Manager',
			array(
				'read'                   => true,
				// Donation capabilities.
				'donorcore_view_donations'         => true,
				'donorcore_export_donations'       => true,
				'donorcore_manage_campaigns'       => true,
				'donorcore_view_donor_analytics'   => true,
				'donorcore_manage_recurring_plans' => true,
				'donorcore_download_receipts'      => true,
				// Limited WordPress capabilities.
				'edit_posts'             => false,
				'delete_posts'           => false,
				'publish_posts'          => false,
				'upload_files'           => false,
			)
		);

		// Add capabilities to Administrator.
		$admin = get_role( 'administrator' );
		if ( $admin ) {
			$admin->add_cap( 'donorcore_view_donations' );
			$admin->add_cap( 'donorcore_export_donations' );
			$admin->add_cap( 'donorcore_manage_campaigns' );
			$admin->add_cap( 'donorcore_view_donor_analytics' );
			$admin->add_cap( 'donorcore_manage_recurring_plans' );
			$admin->add_cap( 'donorcore_download_receipts' );
			
			// Add campaign post type capabilities
			$admin->add_cap( 'edit_donorcore_campaign' );
			$admin->add_cap( 'read_donorcore_campaign' );
			$admin->add_cap( 'delete_donorcore_campaign' );
			$admin->add_cap( 'edit_donorcore_campaigns' );
			$admin->add_cap( 'edit_others_donorcore_campaigns' );
			$admin->add_cap( 'publish_donorcore_campaigns' );
			$admin->add_cap( 'read_private_donorcore_campaigns' );
			$admin->add_cap( 'delete_donorcore_campaigns' );
			$admin->add_cap( 'delete_private_donorcore_campaigns' );
			$admin->add_cap( 'delete_published_donorcore_campaigns' );
			$admin->add_cap( 'delete_others_donorcore_campaigns' );
			$admin->add_cap( 'edit_private_donorcore_campaigns' );
			$admin->add_cap( 'edit_published_donorcore_campaigns' );
		}

		// Add capabilities to Shop Manager.
		$shop_manager = get_role( 'shop_manager' );
		if ( $shop_manager ) {
			$shop_manager->add_cap( 'donorcore_view_donations' );
			$shop_manager->add_cap( 'donorcore_export_donations' );
			$shop_manager->add_cap( 'donorcore_manage_campaigns' );
			$shop_manager->add_cap( 'donorcore_view_donor_analytics' );
			$shop_manager->add_cap( 'donorcore_manage_recurring_plans' );
			$shop_manager->add_cap( 'donorcore_download_receipts' );
		}
	}

	/**
	 * Remove custom roles.
	 */
	public static function remove_roles() {
		remove_role( 'donorcore_donation_manager' );

		// Remove capabilities from Administrator.
		$admin = get_role( 'administrator' );
		if ( $admin ) {
			$admin->remove_cap( 'donorcore_view_donations' );
			$admin->remove_cap( 'donorcore_export_donations' );
			$admin->remove_cap( 'donorcore_manage_campaigns' );
			$admin->remove_cap( 'donorcore_view_donor_analytics' );
			$admin->remove_cap( 'donorcore_manage_recurring_plans' );
			$admin->remove_cap( 'donorcore_download_receipts' );
			
			// Remove campaign post type capabilities
			$admin->remove_cap( 'edit_donorcore_campaign' );
			$admin->remove_cap( 'read_donorcore_campaign' );
			$admin->remove_cap( 'delete_donorcore_campaign' );
			$admin->remove_cap( 'edit_donorcore_campaigns' );
			$admin->remove_cap( 'edit_others_donorcore_campaigns' );
			$admin->remove_cap( 'publish_donorcore_campaigns' );
			$admin->remove_cap( 'read_private_donorcore_campaigns' );
			$admin->remove_cap( 'delete_donorcore_campaigns' );
			$admin->remove_cap( 'delete_private_donorcore_campaigns' );
			$admin->remove_cap( 'delete_published_donorcore_campaigns' );
			$admin->remove_cap( 'delete_others_donorcore_campaigns' );
			$admin->remove_cap( 'edit_private_donorcore_campaigns' );
			$admin->remove_cap( 'edit_published_donorcore_campaigns' );
		}

		// Remove capabilities from Shop Manager.
		$shop_manager = get_role( 'shop_manager' );
		if ( $shop_manager ) {
			$shop_manager->remove_cap( 'donorcore_view_donations' );
			$shop_manager->remove_cap( 'donorcore_export_donations' );
			$shop_manager->remove_cap( 'donorcore_manage_campaigns' );
			$shop_manager->remove_cap( 'donorcore_view_donor_analytics' );
			$shop_manager->remove_cap( 'donorcore_manage_recurring_plans' );
			$shop_manager->remove_cap( 'donorcore_download_receipts' );
		}
	}
}
