<?php
/**
 * CPT Builder - Capabilities tab (easy presets).
 *
 * @package CptBuilder
 *
 * @var array<string, mixed> $definition
 */

namespace CptBuilder;

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

$args     = isset( $definition['args'] ) && is_array( $definition['args'] ) ? $definition['args'] : [];
$key      = (string) ( $definition['key'] ?? '' );
$mode     = (string) ( $args['capability_mode'] ?? '' );
$cap_type = (string) ( $args['capability_type'] ?? 'post' );

if ( $mode === '' || ! isset( Config::capabilityModes()[ $mode ] ) ) {
	if ( $cap_type === 'page' ) {
		$mode = 'page';
	} elseif ( $cap_type === 'post' || $cap_type === '' ) {
		$mode = 'post';
	} else {
		$mode = 'custom';
	}
}

$singular = (string) ( $args['capability_type'] ?? '' );
$plural   = (string) ( $args['capability_type_plural'] ?? '' );
if ( $mode === 'custom' ) {
	if ( $singular === '' || in_array( $singular, [ 'post', 'page' ], true ) ) {
		$singular = $key !== '' ? $key : 'item';
	}
	if ( $plural === '' ) {
		$plural = $singular . 's';
	}
}

$preview_singular = $mode === 'custom' ? $singular : $mode;
$preview_plural   = $mode === 'custom' ? $plural : '';
$preview_caps     = Config::capabilityPreview( $preview_singular, $preview_plural );
$grant_admin      = ! empty( $args['grant_caps_to_admin'] );
$map_meta         = array_key_exists( 'map_meta_cap', $args ) ? ! empty( $args['map_meta_cap'] ) : true;
?>
<h2><?php esc_html_e( 'Capabilities', 'wp-powersuite' ); ?></h2>
<p class="description">
	<?php esc_html_e( 'Choose who can manage this content. Most post types should reuse standard Post permissions.', 'wp-powersuite' ); ?>
</p>

<div class="cptb-cap-modes" role="radiogroup" aria-label="<?php esc_attr_e( 'Capability mode', 'wp-powersuite' ); ?>">
	<?php foreach ( Config::capabilityModes() as $mode_key => $meta ) : ?>
		<label class="cptb-cap-mode<?php echo $mode === $mode_key ? ' is-selected' : ''; ?>">
			<input
				type="radio"
				name="args[capability_mode]"
				value="<?php echo esc_attr( $mode_key ); ?>"
				<?php checked( $mode, $mode_key ); ?>
				data-cptb-cap-mode="<?php echo esc_attr( $mode_key ); ?>"
			>
			<span class="cptb-cap-mode__title"><?php echo esc_html( $meta['label'] ); ?></span>
			<span class="cptb-cap-mode__help"><?php echo esc_html( $meta['help'] ); ?></span>
		</label>
	<?php endforeach; ?>
</div>

<div class="cptb-cap-custom" data-cptb-cap-custom <?php echo $mode === 'custom' ? '' : 'hidden'; ?>>
	<table class="form-table" role="presentation">
		<tbody>
			<tr>
				<th scope="row"><label for="cptb-capability-type"><?php esc_html_e( 'Capability base (singular)', 'wp-powersuite' ); ?></label></th>
				<td>
					<input
						type="text"
						id="cptb-capability-type"
						name="args[capability_type]"
						value="<?php echo esc_attr( $mode === 'custom' ? $singular : '' ); ?>"
						class="regular-text"
						placeholder="<?php echo esc_attr( $key !== '' ? $key : 'book' ); ?>"
						data-cptb-cap-singular
					>
					<p class="description"><?php esc_html_e( 'Used to build permissions such as edit_book. Defaults to the post type key.', 'wp-powersuite' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="cptb-capability-type-plural"><?php esc_html_e( 'Capability base (plural)', 'wp-powersuite' ); ?></label></th>
				<td>
					<input
						type="text"
						id="cptb-capability-type-plural"
						name="args[capability_type_plural]"
						value="<?php echo esc_attr( $mode === 'custom' ? $plural : '' ); ?>"
						class="regular-text"
						placeholder="<?php echo esc_attr( ( $key !== '' ? $key : 'book' ) . 's' ); ?>"
						data-cptb-cap-plural
					>
					<p class="description"><?php esc_html_e( 'Optional. Leave empty to append “s” (or set irregular plurals like boxes).', 'wp-powersuite' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Grant to Administrators', 'wp-powersuite' ); ?></th>
				<td>
					<label class="cptb-toggle" for="cptb-grant-caps-to-admin">
						<input type="checkbox" id="cptb-grant-caps-to-admin" name="args[grant_caps_to_admin]" value="1" <?php checked( $grant_admin ); ?>>
						<span><?php esc_html_e( 'Automatically give Administrators these custom capabilities when you save.', 'wp-powersuite' ); ?></span>
					</label>
				</td>
			</tr>
		</tbody>
	</table>
</div>

<div class="cptb-cap-preview" data-cptb-cap-preview>
	<h3><?php esc_html_e( 'Permissions preview', 'wp-powersuite' ); ?></h3>
	<p class="description">
		<?php
		if ( $mode === 'custom' ) {
			esc_html_e( 'These custom capabilities will be registered for this post type:', 'wp-powersuite' );
		} else {
			esc_html_e( 'This post type will reuse these familiar WordPress capabilities:', 'wp-powersuite' );
		}
		?>
	</p>
	<ul class="cptb-cap-list" data-cptb-cap-list>
		<?php foreach ( $preview_caps as $cap ) : ?>
			<li><code><?php echo esc_html( $cap ); ?></code></li>
		<?php endforeach; ?>
	</ul>
</div>

<details class="cptb-advanced">
	<summary><?php esc_html_e( 'Advanced', 'wp-powersuite' ); ?></summary>
	<table class="form-table" role="presentation">
		<tbody>
			<tr>
				<th scope="row"><?php esc_html_e( 'Map meta capabilities', 'wp-powersuite' ); ?></th>
				<td>
					<label class="cptb-toggle" for="cptb-map-meta-cap">
						<input type="checkbox" id="cptb-map-meta-cap" name="args[map_meta_cap]" value="1" <?php checked( $map_meta ); ?>>
						<span><?php esc_html_e( 'Let WordPress map primitive capabilities automatically (recommended).', 'wp-powersuite' ); ?></span>
					</label>
				</td>
			</tr>
		</tbody>
	</table>
</details>
