<?php

namespace RadiusTheme\RadiusBooking\Elementor\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use RadiusTheme\RadiusBooking\Controllers\ServiceController;

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

class EmployeeListWidget extends Widget_Base {

	public function get_name() {
		return 'rtrb_employee_list';
	}

	public function get_title() {
		return esc_html__( 'Employee List', 'radius-booking' );
	}

	public function get_icon() {
		return 'rtrb-icon-booking';
	}

	public function get_categories() {
		return array( 'radius-booking' );
	}

	public function get_keywords() {
		return array( 'employee', 'staff', 'team', 'booking', 'radius' );
	}

	/**
	 * Service options for the filter dropdown.
	 *
	 * @return array
	 */
	private function get_services_options() {
		$options = array( '' => esc_html__( 'All Services', 'radius-booking' ) );

		try {
			$categories = ( new ServiceController() )->fetchAllServices();
			if ( is_array( $categories ) ) {
				foreach ( $categories as $category ) {
					foreach ( ( $category['services'] ?? array() ) as $service ) {
						$cat_name                    = $category['name'] ?? __( 'Uncategorized', 'radius-booking' );
						$options[ $service['id'] ]   = $cat_name . ' — ' . $service['name'];
					}
				}
			}
		} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
			// Silently fail.
		}

		return $options;
	}

	private function slider_val( $settings, $key, $fallback = 0 ) {
		return ( $settings[ $key ]['size'] ?? $fallback ) . ( $settings[ $key ]['unit'] ?? 'px' );
	}

	protected function register_controls() {
		/* ───────────── CONTENT ───────────── */
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Settings', 'radius-booking' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'layout',
			array(
				'label'   => esc_html__( 'Layout', 'radius-booking' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'grid',
				'options' => array(
					'grid' => esc_html__( 'Grid', 'radius-booking' ),
					'list' => esc_html__( 'List', 'radius-booking' ),
				),
			)
		);

		$this->add_control(
			'columns',
			array(
				'label'     => esc_html__( 'Columns', 'radius-booking' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '3',
				'options'   => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
				),
				'condition' => array(
					'layout' => 'grid',
				),
			)
		);

		$this->add_control(
			'per_page',
			array(
				'label'   => esc_html__( 'Per Page', 'radius-booking' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 9,
				'min'     => 1,
				'max'     => 50,
			)
		);

		$this->add_control(
			'service',
			array(
				'label'          => esc_html__( 'Filter by Service', 'radius-booking' ),
				'type'           => Controls_Manager::SELECT2,
				'default'        => '',
				'options'        => $this->get_services_options(),
				'label_block'    => true,
				'select2options' => array( 'placeholder' => esc_html__( 'All services', 'radius-booking' ) ),
				'description'    => esc_html__( 'Only show staff who provide this service.', 'radius-booking' ),
			)
		);

		$this->add_control(
			'location',
			array(
				'label'       => esc_html__( 'Filter by Location', 'radius-booking' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'description' => esc_html__( 'Only show staff whose location name matches this text.', 'radius-booking' ),
			)
		);

		$this->add_control(
			'show_image',
			array(
				'label'        => esc_html__( 'Show Photo', 'radius-booking' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_services',
			array(
				'label'        => esc_html__( 'Show Services', 'radius-booking' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_location',
			array(
				'label'        => esc_html__( 'Show Location', 'radius-booking' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'show_phone',
			array(
				'label'        => esc_html__( 'Show Phone', 'radius-booking' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'show_description',
			array(
				'label'        => esc_html__( 'Show Description', 'radius-booking' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_button',
			array(
				'label'        => esc_html__( 'Show Book Now Button', 'radius-booking' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'button_text',
			array(
				'label'     => esc_html__( 'Button Text', 'radius-booking' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Book Now', 'radius-booking' ),
				'condition' => array(
					'show_button' => 'yes',
				),
			)
		);

		$this->end_controls_section();

		/* ───────────── STYLE ───────────── */
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Style', 'radius-booking' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'primary_color',
			array(
				'label'   => esc_html__( 'Primary Color', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#0040ff',
			)
		);

		$this->add_control(
			'card_bg_color',
			array(
				'label'   => esc_html__( 'Card Background', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#ffffff',
			)
		);

		$this->add_control(
			'card_border_color',
			array(
				'label'   => esc_html__( 'Card Border', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#e5e7eb',
			)
		);

		$this->add_control(
			'card_border_radius',
			array(
				'label'      => esc_html__( 'Card Radius', 'radius-booking' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'default'    => array(
					'unit' => 'px',
					'size' => 12,
				),
			)
		);

		$this->add_control(
			'card_gap',
			array(
				'label'      => esc_html__( 'Gap Between Cards', 'radius-booking' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'default'    => array(
					'unit' => 'px',
					'size' => 24,
				),
			)
		);

		$this->add_control(
			'image_height',
			array(
				'label'      => esc_html__( 'Image Height', 'radius-booking' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'default'    => array(
					'unit' => 'px',
					'size' => 220,
				),
			)
		);

		$this->add_control(
			'image_fit',
			array(
				'label'       => esc_html__( 'Image Fit', 'radius-booking' ),
				'type'        => Controls_Manager::SELECT,
				'default'     => 'cover',
				'options'     => array(
					'cover'   => esc_html__( 'Cover (fill & crop)', 'radius-booking' ),
					'contain' => esc_html__( 'Contain (show full image)', 'radius-booking' ),
				),
				'description' => esc_html__( 'Cover crops the photo to fill the panel; Contain shows the whole photo without cropping.', 'radius-booking' ),
			)
		);

		$this->add_control(
			'title_color',
			array(
				'label'   => esc_html__( 'Name Color', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#111827',
			)
		);

		$this->add_control(
			'title_font_size',
			array(
				'label'      => esc_html__( 'Name Font Size', 'radius-booking' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'default'    => array(
					'unit' => 'px',
					'size' => 18,
				),
			)
		);

		$this->add_control(
			'meta_color',
			array(
				'label'   => esc_html__( 'Meta Text Color', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#6b7280',
			)
		);

		$this->add_control(
			'button_bg_color',
			array(
				'label'   => esc_html__( 'Button Background', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '',
			)
		);

		$this->add_control(
			'button_text_color',
			array(
				'label'   => esc_html__( 'Button Text', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#ffffff',
			)
		);

		$this->add_control(
			'button_hover_bg_color',
			array(
				'label'   => esc_html__( 'Button Hover Background', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '',
			)
		);

		$this->add_control(
			'button_hover_text_color',
			array(
				'label'   => esc_html__( 'Button Hover Text', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '',
			)
		);

		$this->add_control(
			'button_border_radius',
			array(
				'label'      => esc_html__( 'Button Radius', 'radius-booking' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'default'    => array(
					'unit' => 'px',
					'size' => 8,
				),
			)
		);

		$this->add_control(
			'pagination_active_bg',
			array(
				'label'   => esc_html__( 'Pagination Active Background', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '',
			)
		);

		$this->add_control(
			'pagination_active_text',
			array(
				'label'   => esc_html__( 'Pagination Active Text', 'radius-booking' ),
				'type'    => Controls_Manager::COLOR,
				'default' => '#ffffff',
			)
		);

		$this->end_controls_section();
	}

	protected function render() {
		$s = $this->get_settings_for_display();

		$widget_styles = array(
			'primaryColor'             => $s['primary_color'] ?? '#0040ff',
			'cardBgColor'              => $s['card_bg_color'] ?? '#ffffff',
			'cardBorderColor'          => $s['card_border_color'] ?? '#e5e7eb',
			'cardBorderRadius'         => $this->slider_val( $s, 'card_border_radius', 12 ),
			'cardGap'                  => $this->slider_val( $s, 'card_gap', 24 ),
			'imageHeight'              => $this->slider_val( $s, 'image_height', 220 ),
			'imageFit'                 => ( ( $s['image_fit'] ?? 'cover' ) === 'contain' ) ? 'contain' : 'cover',
			'titleColor'               => $s['title_color'] ?? '#111827',
			'titleFontSize'            => $this->slider_val( $s, 'title_font_size', 18 ),
			'metaColor'                => $s['meta_color'] ?? '#6b7280',
			'buttonBgColor'            => $s['button_bg_color'] ?? '',
			'buttonTextColor'          => $s['button_text_color'] ?? '#ffffff',
			'buttonHoverBgColor'       => $s['button_hover_bg_color'] ?? '',
			'buttonHoverTextColor'     => $s['button_hover_text_color'] ?? '',
			'buttonBorderRadius'       => $this->slider_val( $s, 'button_border_radius', 8 ),
			'paginationActiveBg'       => $s['pagination_active_bg'] ?? '',
			'paginationActiveText'     => $s['pagination_active_text'] ?? '#ffffff',
			'paginationInactiveBg'     => '#ffffff',
			'paginationInactiveText'   => '#4b5563',
			'paginationInactiveBorder' => '#e5e7eb',
			'paginationBorderRadius'   => '8px',
		);

		$display_options = array(
			'showImage'    => 'yes' === ( $s['show_image'] ?? 'yes' ),
			'showServices' => 'yes' === ( $s['show_services'] ?? 'yes' ),
			'showLocation' => 'yes' === ( $s['show_location'] ?? '' ),
			'showPhone'    => 'yes' === ( $s['show_phone'] ?? '' ),
			'showDescription' => 'yes' === ( $s['show_description'] ?? 'yes' ),
			'showButton'   => 'yes' === ( $s['show_button'] ?? 'yes' ),
			'buttonText'   => $s['button_text'] ?? 'Book Now',
		);

		$atts = wp_json_encode(
			array(
				'layout'   => $s['layout'] ?? 'grid',
				'columns'  => $s['columns'] ?? '3',
				'per_page' => $s['per_page'] ?? '9',
				'service'  => $s['service'] ?? '',
				'location' => $s['location'] ?? '',
			)
		);

		$elementor_styles  = wp_json_encode( $widget_styles );
		$elementor_display = wp_json_encode( $display_options );

		echo '<div class="radius-booking">';
		echo '<div class="rtrb-root rtrb-employee-list" data-atts="' . esc_attr( $atts ) . '" data-elementor-styles="' . esc_attr( $elementor_styles ) . '" data-elementor-display="' . esc_attr( $elementor_display ) . '">';
		echo '<div class="rtrb-initial-loader" style="display:flex;align-items:center;justify-content:center;min-height:300px;">';
		echo '<svg width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="animation:rtrb-spin 1s linear infinite;color:#6B7280;"><path d="M21 12a9 9 0 1 1-6.219-8.56"/></svg>';
		echo '</div>';
		echo '</div>';
		echo '</div>';
		echo '<style>@keyframes rtrb-spin{to{transform:rotate(360deg)}}</style>';
	}
}
