<?php

namespace AIPT\Api\Chatbot\Actions;

use AIPT\Api\Chatbot\ChatActionInterface;
use AIPT\Api\Chatbot\ChatbotSettings;

/**
 * View Product action for chatbot.
 *
 * Redirects user to the product page.
 *
 * @package AIPT\Api\Chatbot\Actions
 * @since 2.5.0
 */
class ViewProductAction implements ChatActionInterface {

    /**
     * Get unique action identifier.
     *
     * @return string Action ID.
     */
    public function getId(): string {
        return 'view_product';
    }

    /**
     * Get action label for button.
     *
     * @param string $language Language code for translation.
     * @return string Translated label.
     */
    public function getLabel(string $language = 'en'): string {
        $labels = ChatbotSettings::getLabelsForLanguage($language);
        return $labels['view_product'] ?? __( 'View Product', 'ai-product-tools' );
    }

    /**
     * Get action icon identifier.
     *
     * @return string Icon name.
     */
    public function getIcon(): string {
        return 'eye';
    }

    /**
     * Check if action is available for a specific product.
     *
     * @param int $product_id WooCommerce product ID.
     * @return bool True if action can be performed.
     */
    public function isAvailable(int $product_id): bool {
        $product = wc_get_product($product_id);

        if (!$product) {
            return false;
        }

        return $product->is_visible();
    }

    /**
     * Execute the action.
     *
     * @param int   $product_id WooCommerce product ID.
     * @param array $params     Additional parameters from request.
     * @return array Execution result.
     */
    public function execute(int $product_id, array $params = []): array {
        $product = wc_get_product($product_id);

        if (!$product) {
            return [
                'success' => false,
                'message' => __('Product not found.', 'ai-product-tools'),
            ];
        }

        if (!$product->is_visible()) {
            return [
                'success' => false,
                'message' => __('Product not found.', 'ai-product-tools'),
            ];
        }

        return [
            'success' => true,
            'data' => [
                'action' => 'redirect',
                'url' => $product->get_permalink(),
            ],
        ];
    }

    /**
     * Get action priority for ordering.
     *
     * @return int Priority value.
     */
    public function getPriority(): int {
        return 10;
    }
}
