<?php
/**
 * Built-in variable source implementations.
 *
 * @package 100Prints
 */

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

class Hundred_Prints_Variable_Source_Current_User extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'current_user';
	}

	public function get_label() {
		return 'Current User (Logged In)';
	}

	public function get_fields() {
		return [
			[
				'key'     => 'userField',
				'label'   => 'User Field',
				'type'    => 'select',
				'default' => 'display_name',
				'options' => [
					[ 'label' => 'Display Name', 'value' => 'display_name' ],
					[ 'label' => 'First Name', 'value' => 'first_name' ],
					[ 'label' => 'Last Name', 'value' => 'last_name' ],
					[ 'label' => 'Email Address', 'value' => 'user_email' ],
					[ 'label' => 'Username', 'value' => 'user_login' ],
					[ 'label' => 'User ID', 'value' => 'user_id' ],
					[ 'label' => 'Profile Picture URL', 'value' => 'avatar_url' ],
				],
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$user_id = isset( $context['user_id'] ) ? absint( $context['user_id'] ) : get_current_user_id();
		if ( ! $user_id ) {
			return '';
		}

		$user = get_userdata( $user_id );
		if ( ! $user ) {
			return '';
		}

		$field = sanitize_key( $this->get_rule_value( $rule, [ 'userField', 'sub_key' ], 'display_name' ) );

		switch ( $field ) {
			case 'first_name':
			case 'last_name':
				return get_user_meta( $user_id, $field, true );
			case 'user_email':
				return sanitize_email( $user->user_email );
			case 'user_login':
			case 'display_name':
				return isset( $user->{$field} ) ? $user->{$field} : '';
			case 'user_id':
				return (string) $user_id;
			case 'avatar_url':
				return get_avatar_url( $user_id, [ 'size' => 120 ] );
			default:
				return '';
		}
	}
}

class Hundred_Prints_Variable_Source_User_Meta extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'user_meta';
	}

	public function get_label() {
		return 'User Meta';
	}

	public function get_fields() {
		return [
			[
				'key'         => 'metaKey',
				'label'       => 'Meta Key',
				'type'        => 'text',
				'placeholder' => 'e.g. company_name',
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$user_id  = isset( $context['user_id'] ) ? absint( $context['user_id'] ) : get_current_user_id();
		$meta_key = sanitize_text_field( $this->get_rule_value( $rule, [ 'metaKey', 'sub_key' ] ) );

		return ( $user_id && $meta_key ) ? get_user_meta( $user_id, $meta_key, true ) : '';
	}
}

class Hundred_Prints_Variable_Source_Post_Data extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'post_data';
	}

	public function get_label() {
		return 'Post Data';
	}

	public function get_fields() {
		return [
			[
				'key'     => 'postField',
				'label'   => 'Post Field',
				'type'    => 'select',
				'default' => 'post_title',
				'options' => [
					[ 'label' => 'Post Title', 'value' => 'post_title' ],
					[ 'label' => 'Post Excerpt', 'value' => 'post_excerpt' ],
					[ 'label' => 'Post Author', 'value' => 'post_author_name' ],
					[ 'label' => 'Post Date', 'value' => 'post_date' ],
					[ 'label' => 'Post URL', 'value' => 'post_url' ],
					[ 'label' => 'Post ID', 'value' => 'post_id' ],
				],
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$post_id = isset( $context['post_id'] ) && $context['post_id'] ? absint( $context['post_id'] ) : get_the_ID();
		if ( ! $post_id ) {
			return '';
		}

		$field = sanitize_key( $this->get_rule_value( $rule, [ 'postField', 'sub_key' ], 'post_title' ) );

		switch ( $field ) {
			case 'post_excerpt':
				return get_the_excerpt( $post_id );
			case 'post_author_name':
				$author_id = get_post_field( 'post_author', $post_id );
				return $author_id ? get_the_author_meta( 'display_name', $author_id ) : '';
			case 'post_date':
				return get_the_date( '', $post_id );
			case 'post_url':
				return get_permalink( $post_id );
			case 'post_id':
				return (string) $post_id;
			case 'post_title':
			default:
				return get_the_title( $post_id );
		}
	}
}

class Hundred_Prints_Variable_Source_Post_Meta extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'post_meta';
	}

	public function get_label() {
		return 'Post Meta (Custom Field)';
	}

	public function get_fields() {
		return [
			[
				'key'         => 'metaKey',
				'label'       => 'Meta Key',
				'type'        => 'text',
				'placeholder' => 'e.g. department_name',
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$post_id  = isset( $context['post_id'] ) && $context['post_id'] ? absint( $context['post_id'] ) : get_the_ID();
		$meta_key = sanitize_text_field( $this->get_rule_value( $rule, [ 'metaKey', 'sub_key' ] ) );

		if ( ! $post_id || ! $meta_key ) {
			return '';
		}

		if ( function_exists( 'get_field' ) ) {
			$acf_value = get_field( $meta_key, $post_id );
			if ( null !== $acf_value && false !== $acf_value ) {
				if ( is_array( $acf_value ) && isset( $acf_value['url'] ) ) {
					return $acf_value['url'];
				}

				return is_scalar( $acf_value ) ? $acf_value : wp_json_encode( $acf_value );
			}
		}

		return get_post_meta( $post_id, $meta_key, true );
	}
}

class Hundred_Prints_Variable_Source_Site_Info extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'site_info';
	}

	public function get_label() {
		return 'Site Information';
	}

	public function get_fields() {
		return [
			[
				'key'     => 'siteField',
				'label'   => 'Site Field',
				'type'    => 'select',
				'default' => 'name',
				'options' => [
					[ 'label' => 'Site Title', 'value' => 'name' ],
					[ 'label' => 'Site Tagline', 'value' => 'description' ],
					[ 'label' => 'Site URL', 'value' => 'url' ],
					[ 'label' => 'Admin Email', 'value' => 'admin_email' ],
					[ 'label' => 'Site Language', 'value' => 'language' ],
				],
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$field = sanitize_key( $this->get_rule_value( $rule, [ 'siteField', 'sub_key' ], 'name' ) );

		switch ( $field ) {
			case 'description':
			case 'url':
				return get_bloginfo( $field );
			case 'admin_email':
				return sanitize_email( get_option( 'admin_email' ) );
			case 'language':
				return get_bloginfo( 'language' );
			case 'blogname':
			case 'name':
				return get_bloginfo( 'name' );
			case 'siteurl':
				return get_bloginfo( 'url' );
			case 'blogdescription':
				return get_bloginfo( 'description' );
			default:
				return '';
		}
	}
}

class Hundred_Prints_Variable_Source_System extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'system';
	}

	public function get_label() {
		return 'System Values (Date/Time)';
	}

	public function get_fields() {
		return [
			[
				'key'     => 'systemField',
				'label'   => 'System Field',
				'type'    => 'select',
				'default' => 'current_date',
				'options' => [
					[ 'label' => 'Current Date', 'value' => 'current_date' ],
					[ 'label' => 'Current Date (Long)', 'value' => 'current_date_long' ],
					[ 'label' => 'Current Time', 'value' => 'current_time' ],
					[ 'label' => 'Current Year', 'value' => 'current_year' ],
					[ 'label' => 'Unique ID', 'value' => 'unique_id' ],
				],
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$field = sanitize_key( $this->get_rule_value( $rule, [ 'systemField', 'sub_key' ], 'current_date' ) );

		switch ( $field ) {
			case 'current_date_long':
				return wp_date( get_option( 'date_format' ) );
			case 'current_time':
				return wp_date( get_option( 'time_format' ) );
			case 'current_year':
				return wp_date( 'Y' );
			case 'unique_id':
				return wp_generate_uuid4();
			case 'current_date':
			default:
				return wp_date( 'Y-m-d' );
		}
	}
}

class Hundred_Prints_Variable_Source_Query_Param extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'query_param';
	}

	public function get_label() {
		return 'URL Query Parameter';
	}

	public function get_fields() {
		return [
			[
				'key'         => 'paramKey',
				'label'       => 'Query Parameter Name',
				'type'        => 'text',
				'placeholder' => 'e.g. certificate_name',
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$param_key = sanitize_text_field( $this->get_rule_value( $rule, [ 'paramKey', 'sub_key' ] ) );

		return ( $param_key && isset( $_GET[ $param_key ] ) ) ? sanitize_text_field( wp_unslash( $_GET[ $param_key ] ) ) : '';
	}
}

class Hundred_Prints_Variable_Source_Cookie extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'cookie';
	}

	public function get_label() {
		return 'Cookie Value';
	}

	public function get_fields() {
		return [
			[
				'key'         => 'cookieKey',
				'label'       => 'Cookie Name',
				'type'        => 'text',
				'placeholder' => 'e.g. visitor_name',
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$cookie_key = sanitize_text_field( $this->get_rule_value( $rule, [ 'cookieKey', 'sub_key' ] ) );

		return ( $cookie_key && isset( $_COOKIE[ $cookie_key ] ) ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_key ] ) ) : '';
	}
}

class Hundred_Prints_Variable_Source_Request_Context extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'request_context';
	}

	public function get_label() {
		return 'Request Context';
	}

	public function get_fields() {
		return [
			[
				'key'     => 'contextField',
				'label'   => 'Request Field',
				'type'    => 'select',
				'default' => 'page_url',
				'options' => [
					[ 'label' => 'Current Page URL', 'value' => 'page_url' ],
					[ 'label' => 'Referrer URL', 'value' => 'referrer' ],
					[ 'label' => 'Visitor IP', 'value' => 'ip_address' ],
					[ 'label' => 'User Agent', 'value' => 'user_agent' ],
				],
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		$post_id = isset( $context['post_id'] ) && $context['post_id'] ? absint( $context['post_id'] ) : get_the_ID();
		$field   = sanitize_key( $this->get_rule_value( $rule, [ 'contextField', 'sub_key' ], 'page_url' ) );

		switch ( $field ) {
			case 'referrer':
				return wp_get_referer() ?: '';
			case 'ip_address':
				return isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
			case 'user_agent':
				return isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
			case 'page_url':
			default:
				return $post_id ? get_permalink( $post_id ) : home_url( add_query_arg( [] ) );
		}
	}
}

class Hundred_Prints_Variable_Source_Static extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'static';
	}

	public function get_label() {
		return 'Static Value';
	}

	public function get_fields() {
		return [
			[
				'key'         => 'value',
				'label'       => 'Text Value',
				'type'        => 'text',
				'placeholder' => 'Enter static text...',
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		return $this->get_rule_value( $rule, [ 'value', 'staticValue', 'sub_key' ] );
	}
}

class Hundred_Prints_Variable_Source_Manual extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'manual';
	}

	public function get_label() {
		return 'Manual Visitor Input';
	}

	public function get_fields() {
		return [
			[
				'key'   => 'manualOptions',
				'label' => 'Manual Input Settings',
				'type'  => 'manual',
			],
		];
	}

	public function resolve( array $rule, array $context ) {
		if ( isset( $rule['manualOptions']['defaultValue'] ) ) {
			return $rule['manualOptions']['defaultValue'];
		}

		return $this->get_rule_value( $rule, [ 'staticValue', 'sub_key' ] );
	}
}

class Hundred_Prints_Variable_Source_Form extends Hundred_Prints_Abstract_Variable_Source {

	public function get_key() {
		return 'form';
	}

	public function get_label() {
		return 'Form Input';
	}

	public function get_fields() {
		return [
			[
				'key'         => 'fieldKey',
				'label'       => 'Field Key',
				'type'        => 'text',
				'placeholder' => 'e.g. name, email, phone',
			],
		];
	}

	/**
	 * Server-side resolve returns empty — form values are client-side only.
	 * The Document block's frontend JS reads form inputs directly from the DOM.
	 */
	public function resolve( array $rule, array $context ) {
		return '';
	}
}

