<?php
namespace UM_Elementor\Abstracts;

use Elementor\Controls_Manager;
use Elementor\Widget_Base;
use UM_Elementor\Helpers\UM_Core_Pages;
use UM_Elementor\Helpers\UM_Editor_Placeholder;
use UM_Elementor\Helpers\UM_Profile_Context;

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

abstract class UM_Profile_Base_Widget extends Widget_Base {

	public function get_categories(): array {
		return [ 'um-profile-forms-elementor' ];
	}

	public function get_icon(): string {
		return 'eicon-person';
	}

	public function get_style_depends(): array {
		return [ 'umep-profile-builder' ];
	}

	public function get_description(): string {
		return __( 'Design an Ultimate Member member page in Elementor. Form fields and validation stay in Ultimate Member.', 'ultimate-member-widgets-for-elementor' );
	}

	public function get_keywords(): array {
		return [ 'ultimate member', 'um', 'profile', 'member', 'page' ];
	}

	public function show_in_panel(): bool {
		if ( is_admin() ) {
			$show = true;
		} elseif ( ! function_exists( 'um_is_core_page' ) || ! function_exists( 'UM' ) ) {
			$show = true;
		} else {
			$show = um_is_core_page( 'user' );
		}

		return (bool) apply_filters( 'umep_profile_widgets_show_in_panel', $show );
	}

	protected function render(): void {
		if ( ! function_exists( 'UM' ) ) {
			if ( current_user_can( 'edit_pages' ) ) {
				$this->render_editor_empty_state(
					__( 'Ultimate Member must be active to use this widget.', 'ultimate-member-widgets-for-elementor' )
				);
			}
			return;
		}

		$user_id = $this->get_effective_profile_user_id();

		if ( ! $user_id ) {
			$this->render_no_context_placeholder();
			return;
		}

		if ( ! UM_Profile_Context::setup( $user_id ) ) {
			$this->render_no_context_placeholder();
			return;
		}

		try {
			$this->render_profile_widget( $user_id );
		} finally {
			try {
				UM_Profile_Context::teardown();
			} catch ( \Throwable $e ) {
				if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
					// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
					error_log( '[UMEP] Profile context teardown failed: ' . $e->getMessage() );
				}
			}
		}
	}

	abstract protected function render_profile_widget( int $user_id ): void;

	protected function register_preview_user_control(): void {
		$this->add_control(
			'preview_user_id',
			[
				'label'       => __( 'Preview as User (editor only)', 'ultimate-member-widgets-for-elementor' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'default'     => 0,
				'description' => __( 'Enter a member User ID to preview their profile data while designing. Leave 0 to use the current profile or a sample member.', 'ultimate-member-widgets-for-elementor' ),
			]
		);
	}

	protected function get_effective_profile_user_id(): int {
		$in_design = UM_Profile_Context::is_elementor_edit_mode() || UM_Profile_Context::is_elementor_preview_mode();

		if ( $in_design ) {
			$preview_id = (int) ( $this->get_settings_for_display()['preview_user_id'] ?? 0 );
			if ( $preview_id > 0 && get_userdata( $preview_id ) ) {
				return $preview_id;
			}
		}

		$context_id = UM_Profile_Context::get_profile_user_id();
		if ( $context_id > 0 ) {
			return $context_id;
		}

		if ( $in_design ) {
			return UM_Profile_Context::get_sample_member_user_id();
		}

		return 0;
	}

	protected function is_editor_design_surface(): bool {
		return UM_Profile_Context::is_elementor_edit_mode() || UM_Profile_Context::is_elementor_preview_mode();
	}

	protected function render_editor_empty_state( string $message, string $hint = '' ): void {
		if ( ! $this->is_editor_design_surface() && ! current_user_can( 'edit_pages' ) ) {
			return;
		}

		UM_Editor_Placeholder::render( $this->get_title(), $message, $hint );
	}

	protected function render_no_context_placeholder(): void {
		if ( ! $this->is_editor_design_surface() ) {
			return;
		}

		$this->render_editor_empty_state(
			__( 'No profile member to preview yet.', 'ultimate-member-widgets-for-elementor' ),
			__( 'Add this widget to the UM User Profile page, set Preview as User, or enable Theme Builder.', 'ultimate-member-widgets-for-elementor' )
		);
	}

	protected function settings(): array {
		return $this->get_settings_for_display();
	}

	protected function register_core_page_panel_hint( string $core_page_slug ): void {
		if ( UM_Core_Pages::is_on_core_page( $core_page_slug ) ) {
			return;
		}

		$hint = UM_Core_Pages::get_core_page_panel_hint_html( $core_page_slug, $this->get_title() );
		if ( '' === $hint ) {
			return;
		}

		$this->add_control(
			'umwfe_core_page_panel_hint',
			[
				'type'            => Controls_Manager::RAW_HTML,
				'raw'             => $hint,
				'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning',
			]
		);
	}

	protected function register_um_edit_form_notice(): void {
		$this->add_control(
			'umwfe_edit_form_in_um_notice',
			[
				'type'            => Controls_Manager::RAW_HTML,
				'raw'             => UM_Core_Pages::get_edit_form_notice_html(),
				'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
			]
		);
	}
}
