<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @since 1.0.0
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
namespace Wisewand\Admin;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Helper class for rendering Editorial Calendar panel fields
 * Follows DRY principle for consistent, reusable field rendering
 */
class Editorial_Field_Renderer {

	/**
	 * Render a checkbox field with label
	 */
	public static function checkbox( $name, $label, $checked = false, $icon = '', $help_text = '' ) {
		?>
		<label class="feature-toggle">
			<input type="checkbox" name="<?php echo esc_attr( $name ); ?>" value="1" <?php checked( $checked, true ); ?>>
			<span>
			<?php
			if ( $icon ) {
				echo esc_html( $icon ) . ' ';}
			?>
			<?php echo esc_html( $label ); ?></span>
		</label>
		<?php if ( $help_text ) : ?>
			<p class="field-help"><?php echo esc_html( $help_text ); ?></p>
			<?php
		endif;
	}

	/**
	 * Render a select dropdown field
	 */
	public static function select( $name, $label, $options, $selected = '', $help_text = '', $placeholder = '' ) {
		?>
		<div class="form-col">
			<label><?php echo esc_html( $label ); ?></label>
			<select name="<?php echo esc_attr( $name ); ?>" class="panel-input">
				<?php if ( $placeholder ) : ?>
					<option value=""><?php echo esc_html( $placeholder ); ?></option>
				<?php endif; ?>
				<?php foreach ( $options as $value => $option_label ) : ?>
					<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $selected, $value ); ?>>
						<?php echo esc_html( $option_label ); ?>
					</option>
				<?php endforeach; ?>
			</select>
			<?php if ( $help_text ) : ?>
				<p class="field-help"><?php echo esc_html( $help_text ); ?></p>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Render a text input field
	 */
	public static function text( $name, $label, $value = '', $placeholder = '', $help_text = '', $optional = false ) {
		?>
		<label>
			<?php echo esc_html( $label ); ?>
			<?php if ( $optional ) : ?>
				<span class="optional">(<?php esc_html_e( 'optional', 'wisewand-ai-content-writer-seo-content-generator' ); ?>)</span>
			<?php endif; ?>
		</label>
		<input type="text" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>"
			class="panel-input" placeholder="<?php echo esc_attr( $placeholder ); ?>">
		<?php if ( $help_text ) : ?>
			<p class="field-help"><?php echo esc_html( $help_text ); ?></p>
			<?php
		endif;
	}

	/**
	 * Render a textarea field
	 */
	public static function textarea( $name, $label, $value = '', $rows = 3, $placeholder = '', $help_text = '', $optional = false ) {
		?>
		<label>
			<?php echo esc_html( $label ); ?>
			<?php if ( $optional ) : ?>
				<span class="optional">(<?php esc_html_e( 'optional', 'wisewand-ai-content-writer-seo-content-generator' ); ?>)</span>
			<?php endif; ?>
		</label>
		<textarea name="<?php echo esc_attr( $name ); ?>" rows="<?php echo absint( $rows ); ?>"
			class="panel-input" placeholder="<?php echo esc_attr( $placeholder ); ?>"><?php echo esc_textarea( $value ); ?></textarea>
		<?php if ( $help_text ) : ?>
			<p class="field-help"><?php echo esc_html( $help_text ); ?></p>
			<?php
		endif;
	}

	/**
	 * Render a color picker field
	 */
	public static function color( $name, $label, $value = '#ffffff', $help_text = '' ) {
		?>
		<div class="form-col">
			<label><?php echo esc_html( $label ); ?></label>
			<input type="color" name="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $value ); ?>"
				class="panel-input color-input">
			<?php if ( $help_text ) : ?>
				<p class="field-help"><?php echo esc_html( $help_text ); ?></p>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Render section header
	 */
	public static function section_header( $title, $icon = '', $description = '' ) {
		?>
		<div class="panel-section">
			<h3>
			<?php
			if ( $icon ) {
				echo esc_html( $icon ) . ' ';}
			?>
			<?php echo esc_html( $title ); ?></h3>
			<?php if ( $description ) : ?>
				<p class="section-description"><?php echo esc_html( $description ); ?></p>
			<?php endif; ?>
		<?php
	}

	/**
	 * Close section div
	 */
	public static function section_close() {
		echo '</div>';
	}

	/**
	 * Render CTA repeater field
	 */
	public static function cta_repeater( $ctas = array() ) {
		if ( empty( $ctas ) ) {
			$ctas = array(
				array(
					'anchor' => '',
					'url'    => '',
				),
			);
		}
		?>
		<div id="ctas-container" class="ctas-container">
			<?php foreach ( $ctas as $index => $cta ) : ?>
				<div class="cta-row" data-index="<?php echo esc_attr( $index ); ?>">
					<div class="cta-row-header">
						<span class="cta-number">CTA #<?php echo esc_html( $index + 1 ); ?></span>
						<button type="button" class="btn-remove-cta" <?php echo $index === 0 ? 'style="visibility:hidden;"' : ''; ?>>🗑️</button>
					</div>
					<div class="cta-row-fields">
						<div class="form-col">
							<label><?php esc_html_e( 'Anchor Text', 'wisewand-ai-content-writer-seo-content-generator' ); ?></label>
							<input type="text" name="cta_anchor[]" class="panel-input cta-anchor"
								value="<?php echo esc_attr( $cta['anchor'] ?? '' ); ?>"
								placeholder="<?php esc_attr_e( 'Click here to learn more', 'wisewand-ai-content-writer-seo-content-generator' ); ?>">
						</div>
						<div class="form-col">
							<label><?php esc_html_e( 'Target URL', 'wisewand-ai-content-writer-seo-content-generator' ); ?></label>
							<input type="url" name="cta_url[]" class="panel-input cta-url"
								value="<?php echo esc_url( $cta['url'] ?? '' ); ?>"
								placeholder="https://example.com/product">
						</div>
					</div>
				</div>
			<?php endforeach; ?>
		</div>
		<button type="button" id="add-cta-btn" class="btn-add-field">
			➕ <?php esc_html_e( 'Add Another CTA', 'wisewand-ai-content-writer-seo-content-generator' ); ?>
		</button>
		<?php
	}

	/**
	 * Render social media network checkboxes
	 */
	public static function social_networks( $selected = array() ) {
		if ( empty( $selected ) ) {
			$selected = array( 'instagram', 'facebook', 'x', 'linkedin' );
		}

		$networks = array(
			'instagram' => array(
				'label' => 'Instagram',
				'icon'  => '📸',
			),
			'facebook'  => array(
				'label' => 'Facebook',
				'icon'  => '📘',
			),
			'x'         => array(
				'label' => 'X (Twitter)',
				'icon'  => '🐦',
			),
			'linkedin'  => array(
				'label' => 'LinkedIn',
				'icon'  => '💼',
			),
		);
		?>
		<div class="social-networks-grid">
			<?php foreach ( $networks as $network => $config ) : ?>
				<label class="network-option">
					<input type="checkbox" name="social_networks[]" value="<?php echo esc_attr( $network ); ?>"
						<?php checked( in_array( $network, $selected ) ); ?>>
					<span><?php echo esc_html( $config['icon'] ); ?> <?php echo esc_html( $config['label'] ); ?></span>
				</label>
			<?php endforeach; ?>
		</div>
		<?php
	}

	/**
	 * Render internal links status filter checkboxes
	 */
	public static function internal_links_status( $selected = array( 'publish' ) ) {
		$statuses = array(
			'draft'   => 'Draft',
			'pending' => 'Pending Review',
			'publish' => 'Published',
			'future'  => 'Scheduled',
		);
		?>
		<div class="status-filter-grid">
			<?php foreach ( $statuses as $status => $label ) : ?>
				<label class="status-option">
					<input type="checkbox" name="internal_links_wp_status[]" value="<?php echo esc_attr( $status ); ?>"
						<?php checked( in_array( $status, $selected ) ); ?>>
					<span><?php echo esc_html( $label ); ?></span>
				</label>
			<?php endforeach; ?>
		</div>
		<?php
	}
}
