<?php
/**
 * Footer Contact Widget.
 *
 * Displays a contact column (icon + text rows) for the footer,
 * matching the "Get in Touch" column from the reference design.
 *
 * @package pawwell
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Pawwell_Footer_Contact_Widget' ) ) :

class Pawwell_Footer_Contact_Widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	public function __construct() {
		parent::__construct(
			'pawwell_footer_contact_widget',
			esc_html__( 'Pawwell: Footer Contact', 'pawwell' ),
			array(
				'description' => esc_html__( 'A contact column with icon + text rows (address, phone, email, hours).', 'pawwell' ),
			)
		);
	}

	/**
	 * Available contact icons.
	 *
	 * @return array
	 */
	public function get_icons() {
		return array(
			'none'  => '',
			'pin'   => '<path d="M12 21s7-6 7-11a7 7 0 0 0-14 0c0 5 7 11 7 11z"/><circle cx="12" cy="10" r="2.5"/>',
			'phone' => '<path d="M5 4h4l2 5-3 2a12 12 0 0 0 5 5l2-3 5 2v4a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2z"/>',
			'mail'  => '<rect x="3" y="5" width="18" height="14" rx="2"/><path d="m3 7 9 6 9-6"/>',
			'clock' => '<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/>',
			'user'  => '<circle cx="12" cy="8" r="4"/><path d="M5 21a7 7 0 0 1 14 0"/>',
			'globe' => '<circle cx="12" cy="12" r="9"/><path d="M3 12h18"/><path d="M12 3a14 14 0 0 1 0 18a14 14 0 0 1 0-18"/>',
		);
	}

	/**
	 * Render a contact icon SVG.
	 *
	 * @param string $key Icon key.
	 * @return string SVG markup, empty string if none.
	 */
	public function render_icon( $key ) {
		$icons = $this->get_icons();

		if ( empty( $key ) || 'none' === $key || ! isset( $icons[ $key ] ) || empty( $icons[ $key ] ) ) {
			return '';
		}

		return '<svg class="footer-contact-icon" width="17" height="17" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">' . $icons[ $key ] . '</svg>';
	}

	/**
	 * Front-end display of widget.
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {

		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
		$items = ! empty( $instance['items'] ) ? $instance['items'] : array();

		// Filter out empty items.
		$items = array_filter( $items, function( $item ) {
			return ! empty( $item['text'] );
		});

		if ( empty( $items ) ) {
			return;
		}

		$items = array_values( $items );

		echo wp_kses_post( $args['before_widget'] );

		if ( ! empty( $title ) ) {
			echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $title ) . $args['after_title'] );
		}
		?>
		<ul class="footer-contact">
			<?php foreach ( $items as $item ) :
				$icon_key = ! empty( $item['icon'] ) ? $item['icon'] : 'none';
				$icon_svg = $this->render_icon( $icon_key );
				$text     = $item['text'];
				$is_email = false !== strpos( $text, '@' ) && is_email( $text );
				$is_url   = preg_match( '#^https?://#i', $text );
			?>
				<li class="footer-contact-item">
					<?php echo $icon_svg; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe SVG built internally. ?>
					<span class="footer-contact-text">
						<?php
						if ( $is_email ) {
							echo '<a href="mailto:' . esc_attr( $text ) . '">' . esc_html( $text ) . '</a>';
						} elseif ( $is_url ) {
							echo '<a href="' . esc_url( $text ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $text ) . '</a>';
						} else {
							echo esc_html( $text );
						}
						?>
					</span>
				</li>
			<?php endforeach; ?>
		</ul>
		<?php
		echo wp_kses_post( $args['after_widget'] );
	}

	/**
	 * Back-end widget form.
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {

		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
		$items = ! empty( $instance['items'] ) ? $instance['items'] : array();

		// Ensure at least one empty item.
		if ( empty( $items ) ) {
			$items = array(
				array(
					'icon' => 'pin',
					'text' => '',
				),
			);
		}

		$available_icons = $this->get_icons();
		?>
		<div class="pawwell-footer-contact-widget-form">
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'pawwell' ); ?></label>
				<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $title ); ?>" class="widefat" placeholder="<?php esc_attr_e( 'e.g. Get in Touch', 'pawwell' ); ?>">
			</p>

			<hr style="margin:15px 0;border-top:1px solid #ddd">
			<p><strong><?php esc_html_e( 'Contact Items', 'pawwell' ); ?></strong></p>

			<div class="pawwell-footer-contact-items">
				<?php foreach ( $items as $i => $item ) :
					$current_icon = isset( $item['icon'] ) ? $item['icon'] : 'none';
				?>
				<div class="pawwell-footer-contact-item-field">
					<p>
						<label><?php esc_html_e( 'Icon:', 'pawwell' ); ?></label>
						<select name="<?php echo esc_attr( $this->get_field_name( 'items' ) ); ?>[<?php echo esc_attr( $i ); ?>][icon]" class="widefat pawwell-footer-contact-icon-select">
							<?php foreach ( $available_icons as $icon_key => $icon_path ) : ?>
								<option value="<?php echo esc_attr( $icon_key ); ?>" <?php selected( $current_icon, $icon_key ); ?>><?php echo esc_html( ucfirst( $icon_key ) ); ?></option>
							<?php endforeach; ?>
						</select>
					</p>
					<p>
						<label><?php esc_html_e( 'Text:', 'pawwell' ); ?></label>
						<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'items' ) ); ?>[<?php echo esc_attr( $i ); ?>][text]" value="<?php echo esc_attr( isset( $item['text'] ) ? $item['text'] : '' ); ?>" class="widefat pawwell-footer-contact-text-input" placeholder="<?php esc_attr_e( 'e.g. 123 Pet Care Ave, San Francisco', 'pawwell' ); ?>">
					</p>
					<button type="button" class="button pawwell-footer-contact-remove-btn" style="margin-bottom:10px;color:#d63639;"><?php esc_html_e( '✕ Remove', 'pawwell' ); ?></button>
					<hr style="margin:10px 0;border-top:1px solid #eee">
				</div>
				<?php endforeach; ?>
			</div>

			<button type="button" class="button pawwell-footer-contact-add-btn"><?php esc_html_e( '+ Add Contact Item', 'pawwell' ); ?></button>
		</div>
		<?php
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 * @return array Updated safe values.
	 */
	public function update( $new_instance, $old_instance ) {

		$instance = array();

		$instance['title'] = ! empty( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '';
		$instance['items'] = array();

		if ( ! empty( $new_instance['items'] ) && is_array( $new_instance['items'] ) ) {
			$valid_icons = array_keys( $this->get_icons() );
			foreach ( $new_instance['items'] as $item ) {
				$text = isset( $item['text'] ) ? sanitize_text_field( $item['text'] ) : '';
				$icon = isset( $item['icon'] ) ? sanitize_key( $item['icon'] ) : 'none';

				if ( ! in_array( $icon, $valid_icons, true ) ) {
					$icon = 'none';
				}

				if ( ! empty( $text ) ) {
					$instance['items'][] = array(
						'icon' => $icon,
						'text' => $text,
					);
				}
			}
		}

		return $instance;
	}
}
endif;

if ( ! function_exists( 'pawwell_register_footer_contact_widget' ) ) :
/**
 * Register the Footer Contact widget.
 */
function pawwell_register_footer_contact_widget() {
	register_widget( 'Pawwell_Footer_Contact_Widget' );
}
endif;
add_action( 'widgets_init', 'pawwell_register_footer_contact_widget' );
