<?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', 'wppowersuite-lite' ); ?></h2>
<p class="description">
	<?php esc_html_e( 'Choose who can manage this content. Most post types should reuse standard Post permissions.', 'wppowersuite-lite' ); ?>
</p>

<div class="cptb-cap-modes" role="radiogroup" aria-label="<?php esc_attr_e( 'Capability mode', 'wppowersuite-lite' ); ?>">
	<?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)', 'wppowersuite-lite' ); ?></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.', 'wppowersuite-lite' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="cptb-capability-type-plural"><?php esc_html_e( 'Capability base (plural)', 'wppowersuite-lite' ); ?></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).', 'wppowersuite-lite' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Grant to Administrators', 'wppowersuite-lite' ); ?></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.', 'wppowersuite-lite' ); ?></span>
					</label>
				</td>
			</tr>
		</tbody>
	</table>
</div>

<div class="cptb-cap-preview" data-cptb-cap-preview>
	<h3><?php esc_html_e( 'Permissions preview', 'wppowersuite-lite' ); ?></h3>
	<p class="description">
		<?php
		if ( $mode === 'custom' ) {
			esc_html_e( 'These custom capabilities will be registered for this post type:', 'wppowersuite-lite' );
		} else {
			esc_html_e( 'This post type will reuse these familiar WordPress capabilities:', 'wppowersuite-lite' );
		}
		?>
	</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', 'wppowersuite-lite' ); ?></summary>
	<table class="form-table" role="presentation">
		<tbody>
			<tr>
				<th scope="row"><?php esc_html_e( 'Map meta capabilities', 'wppowersuite-lite' ); ?></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).', 'wppowersuite-lite' ); ?></span>
					</label>
				</td>
			</tr>
		</tbody>
	</table>
</details>
