<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
class Sifency_Welcome_Admin_Dashboard {

	public function render_welcome_page() {
		if ( isset( $_POST['sifency_subscribe_submit'] ) && check_admin_referer( 'sifency_subscribe', 'sifency_subscribe_nonce' ) ) {
			$email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '';
			if ( is_email( $email ) ) {
				$subscribers = get_option( 'sifency_subscribers', array() );
				if ( ! in_array( $email, $subscribers, true ) ) {
					$subscribers[] = $email;
					update_option( 'sifency_subscribers', $subscribers );
				}
			}
		}

		if ( isset( $_POST['sifency_feature_request_submit'] ) && check_admin_referer( 'sifency_feature_request', 'sifency_feature_nonce' ) ) {
			$feature_email   = isset( $_POST['feature_email'] ) ? sanitize_email( $_POST['feature_email'] ) : '';
			$feature_request = isset( $_POST['feature_request'] ) ? sanitize_textarea_field( $_POST['feature_request'] ) : '';

			if ( is_email( $feature_email ) && ! empty( $feature_request ) ) {
				$admin_email = get_option( 'admin_email' );
				$subject     = '[Sifency Addons] Feature Request';
				$message     = sprintf(
					/* translators: 1: submitter email address, 2: the feature request text */
					__(
						"A feature request has been submitted:\n\nFrom: %1\$s\n\nRequest:\n%2\$s",
						'sifency-addons'
					),
					esc_html( $feature_email ),
					esc_html( $feature_request )
				);
				$headers = array( 'Content-Type: text/plain; charset=UTF-8', 'From: ' . esc_html( $feature_email ) );

				wp_mail( $admin_email, $subject, $message, $headers );

				$feature_requests   = get_option( 'sifency_feature_requests', array() );
				$feature_requests[] = array(
					'email'   => $feature_email,
					'request' => $feature_request,
					'date'    => current_time( 'mysql' ),
				);
				update_option( 'sifency_feature_requests', $feature_requests );
			}
		}

		// Get total widgets from sifency-widgets.php configuration
		$total_widgets  = 0;
		$active_widgets = 0;
		$widget_list    = array();

		if ( class_exists( '\Sifency_Addons\Includes\Elementor\Sifency_Addons_Widget_Registry' ) ) {
			$categorized_widgets = \Sifency_Addons\Includes\Elementor\Sifency_Addons_Widget_Registry::get_categorized();
		} else {
			$categorized_widgets = include SIFENCY_ADDONS_DIR . 'includes/elementor/sifency-widgets.php';
			$categorized_widgets = apply_filters( 'sifency_addons_widget_list', $categorized_widgets );
		}

		if ( is_array( $categorized_widgets ) ) {
			foreach ( $categorized_widgets as $category_widgets ) {
				if ( is_array( $category_widgets ) ) {
					$total_widgets += count( $category_widgets );
					foreach ( $category_widgets as $widget_id => $widget_data ) {
						$widget_list[] = $widget_id;
					}
				}
			}
		}

		$options = get_option( 'sifency_addons_settings', array() );
		if ( isset( $options['widgets'] ) && is_array( $options['widgets'] ) ) {
			foreach ( $options['widgets'] as $widget_id => $status ) {
				if ( 'on' === $status ) {
					$active_widgets++;
				}
			}
		} else {
			$active_widgets = $total_widgets;
		}
		?>

	<div class="sifency-admin-wrap">
		<div class="sifency-dashboard-layout admin-land">
			<div class="sifency-main-content">
				<div class="d-flex sifency-dashboard-header">
					<div class="sifency-welcome-banner">
						<?php if ( ! Sifency_Addons_Theme_Builder_Master::sifency_addons_selective_environment() ) : ?>
						<div class="addons-user-info">
							<div class="user-thumbnail">
								<?php $current_user = wp_get_current_user(); ?>
								<?php echo get_avatar( $current_user->ID, 60 ); ?>
							</div>
							<div class="sifency-user-name">
								<h3><?php /* translators: %s: user's display name */ printf( esc_html__( 'Welcome, %s', 'sifency-addons' ), esc_html( $current_user->display_name ) ); ?>
								</h3>
								<?php if ( ! Sifency_Addons_Theme_Builder_Master::sync_environment_state() ) : ?>
								<span class="sifency-pro-badge sifency-badge">FREE USER</span>
								<?php else : ?>
								<span class="sifency-free-badge sifency-badge">ACTIVATED</span>
								<?php endif; ?>
							</div>
						</div>
						<h2><?php esc_html_e( 'Complete web design Ecosystem for WordPress', 'sifency-addons' ); ?></h2>
						<p><?php esc_html_e( 'Theme Builder, 250+ widgets, custom post types, and pre-built templates. Everything you need to create professional websites without code.', 'sifency-addons' ); ?>
						</p>
						<?php endif; ?>
						
						<div class="sifency-land-quick-action">

							<!-- Dashboard summary cards -->
							<div class="sifency-summary-cards">
								<div class="sifency-summary-card summary-widet">
									<div class="sifency-summary-value"><?php echo esc_html( $total_widgets ); ?></div>
									<h4><?php esc_html_e( 'Total Widgets', 'sifency-addons' ); ?></h4>
								</div>
								<div class="sifency-summary-card summary-widet">
									<div class="sifency-summary-value"><?php echo esc_html( $active_widgets ); ?></div>
									<h4><?php esc_html_e( 'Active Widgets', 'sifency-addons' ); ?></h4>
								</div>
							</div>
							<?php if ( ! Sifency_Addons_Theme_Builder_Master::sifency_addons_selective_environment() ) : ?>
							<div class="sifency-rating-cta">
								<h4><?php esc_html_e( 'Rate The Sifency Addons', 'sifency-addons' ); ?></h4>
								<a href="https://wordpress.org/support/plugin/sifency-addons/reviews/#new-post" target="_blank" class="sifency-rating sifency-admin-btn">★★★★★</a>
							</div>
							<?php endif; ?>
						</div>

					</div>
					<?php
					$post_summary_file = __DIR__ . '/class-post-summary.php';
					if ( file_exists( $post_summary_file ) ) {
						include_once $post_summary_file;
						if ( function_exists( 'sifency_addons_render_post_summary' ) ) {
							sifency_addons_render_post_summary();
						}
					}
					?>
				</div>
				<?php
					$themebuilder_summary_file = __DIR__ . '/class-themebuilder.php';
				if ( file_exists( $themebuilder_summary_file ) ) {
					include_once $themebuilder_summary_file;
					if ( function_exists( 'sifency_addons_render_themebuilder_summary' ) ) {
						sifency_addons_render_themebuilder_summary();
					}
				}
				?>

				<?php if ( ! class_exists( 'Sifency_Essential' ) ) : ?>
				<div class="sifency-dashboard-cards-middle">
					<div class="sifency-card sifency-quickstart">

						<div class="sifency-quick-links">
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-theme-builder' ) ); ?>"
								class="sifency-quick-link-item sifency-admin-btn">
								<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
								<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 16.875h3.375m0 0h3.375m-3.375 0V13.5m0 3.375v3.375M6 10.5h2.25a2.25 2.25 0 0 0 2.25-2.25V6a2.25 2.25 0 0 0-2.25-2.25H6A2.25 2.25 0 0 0 3.75 6v2.25A2.25 2.25 0 0 0 6 10.5Zm0 9.75h2.25A2.25 2.25 0 0 0 10.5 18v-2.25a2.25 2.25 0 0 0-2.25-2.25H6a2.25 2.25 0 0 0-2.25 2.25V18A2.25 2.25 0 0 0 6 20.25Zm9.75-9.75H18a2.25 2.25 0 0 0 2.25-2.25V6A2.25 2.25 0 0 0 18 3.75h-2.25A2.25 2.25 0 0 0 13.5 6v2.25a2.25 2.25 0 0 0 2.25 2.25Z" />
								</svg>
								<div>
									<strong><?php esc_html_e( 'Theme Builder', 'sifency-addons' ); ?></strong>
								</div>
							</a>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-addons-widgets' ) ); ?>"
								class="sifency-quick-link-item sifency-admin-btn">
								<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
								<path stroke-linecap="round" stroke-linejoin="round" d="M3 8.25V18a2.25 2.25 0 0 0 2.25 2.25h13.5A2.25 2.25 0 0 0 21 18V8.25m-18 0V6a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 6v2.25m-18 0h18M5.25 6h.008v.008H5.25V6ZM7.5 6h.008v.008H7.5V6Zm2.25 0h.008v.008H9.75V6Z" />
								</svg>

								<div>
									<strong><?php esc_html_e( 'Widgets', 'sifency-addons' ); ?></strong>
								</div>
							</a>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-templates-shortcode' ) ); ?>"
								class="sifency-quick-link-item sifency-admin-btn">
								<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
								<path stroke-linecap="round" stroke-linejoin="round" d="M6.429 9.75 2.25 12l4.179 2.25m0-4.5 5.571 3 5.571-3m-11.142 0L2.25 7.5 12 2.25l9.75 5.25-4.179 2.25m0 0L21.75 12l-4.179 2.25m0 0 4.179 2.25L12 21.75 2.25 16.5l4.179-2.25m11.142 0-5.571 3-5.571-3" />
								</svg>

								<div>
									<strong><?php esc_html_e( 'Templates', 'sifency-addons' ); ?></strong>
								</div>
							</a>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-settings' ) ); ?>"
								class="sifency-quick-link-item sifency-admin-btn">
								<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
								<path stroke-linecap="round" stroke-linejoin="round" d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z" />
								<path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" />
								</svg>

								<div>
									<strong><?php esc_html_e( 'Global Settings', 'sifency-addons' ); ?></strong>
								</div>
							</a>
						</div>
					</div>
					<div class="sifency-card">
						<div class="sifency-feature-grid">

							<!-- Feature 1 -->
							<div class="sifency-feature-item">
								<div class="sifency-feature-icon">
									<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
									<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6A2.25 2.25 0 0 1 6 3.75h2.25A2.25 2.25 0 0 1 10.5 6v2.25a2.25 2.25 0 0 1-2.25 2.25H6a2.25 2.25 0 0 1-2.25-2.25V6ZM3.75 15.75A2.25 2.25 0 0 1 6 13.5h2.25a2.25 2.25 0 0 1 2.25 2.25V18a2.25 2.25 0 0 1-2.25 2.25H6A2.25 2.25 0 0 1 3.75 18v-2.25ZM13.5 6a2.25 2.25 0 0 1 2.25-2.25H18A2.25 2.25 0 0 1 20.25 6v2.25A2.25 2.25 0 0 1 18 10.5h-2.25a2.25 2.25 0 0 1-2.25-2.25V6ZM13.5 15.75a2.25 2.25 0 0 1 2.25-2.25H18a2.25 2.25 0 0 1 2.25 2.25V18A2.25 2.25 0 0 1 18 20.25h-2.25A2.25 2.25 0 0 1 13.5 18v-2.25Z" />
									</svg>
								</div>
								<h4><?php esc_html_e( 'Premium Widgets', 'sifency-addons' ); ?></h4>
								<p><?php esc_html_e( 'Unlock 50+ advanced widgets including forms, sliders, and post grids.', 'sifency-addons' ); ?>
								</p>
								<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-addons-widgets&tab=widgets' ) ); ?>"
									class="button button-secondary"><?php esc_html_e( 'Explore Widgets', 'sifency-addons' ); ?></a>
							</div>

							<!-- Feature 2 -->
							<div class="sifency-feature-item">
								<div class="sifency-feature-icon">
									 <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
									<path stroke-linecap="round" stroke-linejoin="round" d="M3 8.25V18a2.25 2.25 0 0 0 2.25 2.25h13.5A2.25 2.25 0 0 0 21 18V8.25m-18 0V6a2.25 2.25 0 0 1 2.25-2.25h13.5A2.25 2.25 0 0 1 21 6v2.25m-18 0h18M5.25 6h.008v.008H5.25V6ZM7.5 6h.008v.008H7.5V6Zm2.25 0h.008v.008H9.75V6Z" />
									</svg>
								</div>
								<h4><?php esc_html_e( 'Prebuild Websites', 'sifency-addons' ); ?></h4>
								<p><?php esc_html_e( 'Unlock 50+ advanced prebuild websites full package of your maximum needs.', 'sifency-addons' ); ?>
								</p>
								<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-prebuild-website' ) ); ?>"
									class="button button-secondary"><?php esc_html_e( 'Explore Websites', 'sifency-addons' ); ?></a>
							</div>

							<!-- Feature 3 -->
							<div class="sifency-feature-item">
								<div class="sifency-feature-icon">
									<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
									<path stroke-linecap="round" stroke-linejoin="round" d="M6 6.878V6a2.25 2.25 0 0 1 2.25-2.25h7.5A2.25 2.25 0 0 1 18 6v.878m-12 0c.235-.083.487-.128.75-.128h10.5c.263 0 .515.045.75.128m-12 0A2.25 2.25 0 0 0 4.5 9v.878m13.5-3A2.25 2.25 0 0 1 19.5 9v.878m0 0a2.246 2.246 0 0 0-.75-.128H5.25c-.263 0-.515.045-.75.128m15 0A2.25 2.25 0 0 1 21 12v6a2.25 2.25 0 0 1-2.25 2.25H5.25A2.25 2.25 0 0 1 3 18v-6c0-.98.626-1.813 1.5-2.122" />
									</svg>
								</div>
								<h4><?php esc_html_e( 'Prebuilt Themes', 'sifency-addons' ); ?></h4>
								<p><?php esc_html_e( 'Import professionally designed websites with a single click.', 'sifency-addons' ); ?>
								</p>
								<a href="<?php echo esc_url( admin_url( 'admin.php?page=sifency-themes' ) ); ?>"
									class="button button-secondary"><?php esc_html_e( 'See Themes', 'sifency-addons' ); ?></a>
							</div>

						</div>
					</div>
				</div>
				
					<?php if ( ! Sifency_Addons_Theme_Builder_Master::sync_environment_state() ) : ?>
				<div class="sifency-addons-main-banner">
					<div class="sifency-banner-content">
						<div class="sifency-banner-text">
							<h2>Go <span>Pro</span>,<br>Go <span>Limitless</span></h2>
							<p><?php esc_html_e( 'Unlock 250+ powerful widgets, including forms, price tables, and sliders to take your designs to the next level.', 'sifency-addons' ); ?>
							</p>
							<a href="#"
								class="button button-primary"><?php esc_html_e( 'Upgrade To Pro', 'sifency-addons' ); ?></a>
						</div>
						<div class="sifency-banner-image">
							<img src="<?php echo esc_url( 'https://sifency-addons-images.vercel.app/represent/banner-1.png' ); ?>"
								alt="<?php esc_attr_e( 'Sifency Pro', 'sifency-addons' ); ?>" />
						</div>
					</div>
				</div>
				<?php endif; ?>

				<?php endif; ?>

			</div>
			<?php if ( ! Sifency_Addons_Theme_Builder_Master::sifency_addons_selective_environment() ) : ?>
			<!-- SIDEBAR AREA -->
			<div class="sifency-sidebar">

				<div class="sifency-card">
					<h4><?php esc_html_e( 'Need Help?', 'sifency-addons' ); ?></h4>
					<p class="sifency-subtitle">
						<?php esc_html_e( 'Open a support ticket and our team will assist you.', 'sifency-addons' ); ?></p>
					<a href="https://wordpress.org/support/plugin/sifency-addons" target="_blank" rel="noopener" class="button button-secondary"><?php esc_html_e( 'Raise Ticket', 'sifency-addons' ); ?></a>
				</div>

				<div class="sifency-card">
					<h4><?php esc_html_e( 'Documentation', 'sifency-addons' ); ?></h4>
					<p class="sifency-subtitle">
						<?php esc_html_e( 'Read the docs to learn how to use Sifency Addons.', 'sifency-addons' ); ?></p>
					<a href="https://sifency.com/sifency-addons/doc" target="_blank" rel="noopener" class="button button-secondary"><?php esc_html_e( 'Read Now', 'sifency-addons' ); ?></a>
				</div>

				<div class="sifency-card">
					<h4><?php esc_html_e( 'Join Community', 'sifency-addons' ); ?></h4>
					<p class="sifency-subtitle">
						<?php esc_html_e( 'Connect with other users in our community.', 'sifency-addons' ); ?></p>
					<a href="https://www.linkedin.com/company/sifency" target="_blank" rel="noopener" class="button button-secondary"><?php esc_html_e( 'Join Now', 'sifency-addons' ); ?></a>
				</div>

				<!-- FEATURE REQUEST -->
				<div class="sifency-card">
					<h4><?php esc_html_e( 'Request a Feature', 'sifency-addons' ); ?></h4>
					<p class="sifency-subtitle">
						<?php esc_html_e( 'Have an idea? Send us your feature request and we\'ll review it.', 'sifency-addons' ); ?>
					</p>
					<form class="sifency-feature-request-form" method="post" action="">
						<?php wp_nonce_field( 'sifency_feature_request', 'sifency_feature_nonce' ); ?>
						
						<p>
							<input type="email" name="feature_email"
								placeholder="<?php esc_attr_e( 'Your Email', 'sifency-addons' ); ?>" 
								value="<?php echo esc_attr( wp_get_current_user()->user_email ); ?>" required>
						</p>
						<p>
							<textarea name="feature_request"
								placeholder="<?php esc_attr_e( 'Describe your feature request...', 'sifency-addons' ); ?>"
								rows="4" required></textarea>
						</p>
						<button type="submit" name="sifency_feature_request_submit"
							class="button button-primary"><?php esc_html_e( 'Send Request', 'sifency-addons' ); ?></button>
					</form>
				</div>
				
				<!-- MAILCHIMP SUBSCRIBE -->
				<div class="sifency-card">
					<h4><?php esc_html_e( 'Stay Updated', 'sifency-addons' ); ?></h4>
					<p class="sifency-subtitle">
						<?php esc_html_e( 'Get tips, tutorials, and new feature updates directly to your inbox.', 'sifency-addons' ); ?>
					</p>
					<form class="sifency-subscribe-form" action="" method="post">
						<?php wp_nonce_field( 'sifency_subscribe', 'sifency_subscribe_nonce' ); ?>
						
						<p>
							<input class="sifency-admin-btn" type="email" name="email" 
							placeholder="Your Email Address" required>
						</p>
						
						<button type="submit" name="sifency_subscribe_submit"
							class="button button-primary"><?php esc_html_e( 'Subscribe', 'sifency-addons' ); ?></button>
					</form>
				</div>
				<div class="sifency-summary-card sifency-social-card sifency-card">
					<h4><?php esc_html_e( 'We are active on', 'sifency-addons' ); ?></h4>
					<div class="sifency-social-icons">
						<a href="https://www.facebook.com/sifency" aria-label="Facebook"><svg width="11" height="20" viewBox="0 0 11 20" fill="none" xmlns="http://www.w3.org/2000/svg">
						<path d="M7 11.5H9.5L10.5 7.5H7V5.5C7 4.47 7 3.5 9 3.5H10.5V0.14C10.174 0.0970001 8.943 0 7.643 0C4.928 0 3 1.657 3 4.7V7.5H0V11.5H3V20H7V11.5Z" fill="white"/>
						</svg>
						</a>
						<a href="https://x.com/HelloSifency" aria-label="X"><svg width="1200" height="1227" viewBox="0 0 1200 1227" fill="none" xmlns="http://www.w3.org/2000/svg">
							<path d="M714.163 519.284L1160.89 0H1055.03L667.137 450.887L357.328 0H0L468.492 681.821L0 1226.37H105.866L515.491 750.218L842.672 1226.37H1200L714.137 519.284H714.163ZM569.165 687.828L521.697 619.934L144.011 79.6944H306.615L611.412 515.685L658.88 583.579L1055.08 1150.3H892.476L569.165 687.854V687.828Z" fill="white"/>
							</svg></a>
						<a href="https://www.instagram.com/sifency/" aria-label="Instagram"><span
								class="dashicons dashicons-instagram sifency-admin-btn"></span></a>
						<a href="https://www.linkedin.com/company/sifency" aria-label="LinkedIn"><svg width="20" height="18" viewBox="0 0 20 18" fill="none" xmlns="http://www.w3.org/2000/svg">
						<path d="M4 2.001C3.99974 2.53143 3.78877 3.04004 3.41351 3.41492C3.03825 3.78981 2.52943 4.00027 1.999 4C1.46857 3.99974 0.959965 3.78877 0.585079 3.41351C0.210194 3.03825 -0.000264966 2.52943 2.50361e-07 1.999C0.000265467 1.46857 0.211233 0.959965 0.586494 0.585079C0.961754 0.210194 1.47057 -0.000264966 2.001 2.50361e-07C2.53143 0.000265467 3.04004 0.211233 3.41492 0.586494C3.78981 0.961754 4.00027 1.47057 4 2.001ZM4.06 5.481H0.0600002V18.001H4.06V5.481ZM10.38 5.481H6.4V18.001H10.34V11.431C10.34 7.771 15.11 7.431 15.11 11.431V18.001H19.06V10.071C19.06 3.901 12 4.131 10.34 7.161L10.38 5.481Z" fill="white"/>
						</svg>
						</a>
					</div>
				</div>
			</div>
			<?php endif; ?>

		</div>

	</div>

		<?php
	}
}
