<?php

namespace AIEO\Claude\Abilities;

defined( 'ABSPATH' ) || exit;

class GetCustomerProfileAbility {

	const ABILITY_NAME = 'aieo/get-customer-profile';

	public static function register() {
		wp_register_ability(
			self::ABILITY_NAME,
			array(
				'label'               => __( 'Get customer profile', 'ai-eshop-optimizer' ),
				'description'         => __( "Return a customer's structured profile — inferred priority archetype, top brand affinities, wishlisted brands, rated brands, recent rating activity. Returns the pseudonym `Customer #N` only; real names and emails NEVER leave the server.", 'ai-eshop-optimizer' ),
				'category'            => \AIEO\Claude\Bootstrap::CATEGORY,
				'input_schema'        => self::input_schema(),
				'output_schema'       => array( 'type' => 'object' ),
				'execute_callback'    => array( __CLASS__, 'execute' ),
				'permission_callback' => array( __CLASS__, 'permission_check' ),
				'meta'                => array(
					'annotations'  => array(
						'readonly'   => true,
						'idempotent' => true,
					),
					'show_in_rest' => true,
				),
			)
		);
	}

	public static function permission_check( $input = null ) {
		unset( $input );
		return current_user_can( 'manage_woocommerce' );
	}

	private static function input_schema() {
		return array(
			'type'       => 'object',
			'required'   => array( 'customer_id' ),
			'properties' => array(
				'customer_id' => array(
					'type'    => 'integer',
					'minimum' => 1,
				),
			),
		);
	}

	public static function execute( $input ) {
		$input   = is_array( $input ) ? $input : array();
		$user_id = isset( $input['customer_id'] ) ? (int) $input['customer_id'] : 0;
		if ( $user_id <= 0 ) {
			return new \WP_Error( 'aieo_invalid_input', __( 'customer_id is required.', 'ai-eshop-optimizer' ) );
		}

		$profile = array(
			'customer_id' => $user_id,
			'pseudonym'   => 'Customer #' . $user_id,
		);

		if ( class_exists( 'AIEO_DMM_Surveys' ) ) {
			$priorities = \AIEO_DMM_Surveys::get_customer_priority_profile( $user_id );
			$profile['priority'] = $priorities;
		}

		if ( class_exists( 'AIEO_Customer_Intelligence_Query' ) ) {
			$profile['top_brand_ids']        = \AIEO_Customer_Intelligence_Query::get_top_brands( $user_id, 5 );
			$profile['wishlisted_brand_ids'] = \AIEO_Customer_Intelligence_Query::get_wishlisted_brands( $user_id );
			$profile['rated_brand_ids']      = \AIEO_Customer_Intelligence_Query::get_rated_brands( $user_id );
			$profile['has_brand_affinity']   = \AIEO_Customer_Intelligence_Query::has_brand_affinity( $user_id );
		}

		
		if ( class_exists( 'AIEO_Core_Brand_Vitals' ) && ! empty( $profile['top_brand_ids'] ) ) {
			$cbv    = \AIEO_Core_Brand_Vitals::instance();
			$brands = $cbv->get_by_ids( (array) $profile['top_brand_ids'] );
			$profile['top_brands'] = array_map(
				static function ( $b ) {
					return array(
						'brand_id'      => (int) ( $b['brand_id'] ?? 0 ),
						'name'          => (string) ( $b['brand_name'] ?? '' ),
						'product_count' => (int) ( $b['product_count'] ?? 0 ),
					);
				},
				$brands
			);
		}

		return $profile;
	}
}
