<?php

namespace EasyLinkAutomations\Triggers\WooCommerce;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a note is added to a WooCommerce order.
 */
final class OrderNoteAddedTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'woocommerce/order_note_added';
    }

    public static function getName(): string
    {
        return __('Order note added', 'easylink-automations');
    }

    public static function getDescription(): string
    {
        return __('Triggers when a note is added to a WooCommerce order', 'easylink-automations');
    }

    public static function getCategory(): string
    {
        return 'WooCommerce';
    }

    public static function getIcon(): string
    {
        return 'woocommerce';
    }


    public static function isAvailable(): bool
    {
        return class_exists('WooCommerce');
    }

    public static function getOutputFields(): array
    {
        return [
            'order_id'             => ['label' => 'Order ID', 'type' => 'number'],
            'order_number'         => ['label' => 'Order Number', 'type' => 'string'],
            'order_status'         => ['label' => 'Order Status', 'type' => 'string'],
            'order_total'          => ['label' => 'Order Total', 'type' => 'string'],
            'order_currency'       => ['label' => 'Order Currency', 'type' => 'string'],
            'order_date'           => ['label' => 'Order Date', 'type' => 'datetime'],
            'billing_first_name'   => ['label' => 'Billing First Name', 'type' => 'string'],
            'billing_last_name'    => ['label' => 'Billing Last Name', 'type' => 'string'],
            'billing_email'        => ['label' => 'Billing Email', 'type' => 'email'],
            'note_id'              => ['label' => 'Note ID', 'type' => 'number'],
            'note_content'         => ['label' => 'Note Content', 'type' => 'string'],
            'note_author'          => ['label' => 'Note Author', 'type' => 'string'],
            'note_date'            => ['label' => 'Note Date', 'type' => 'datetime'],
            'note_type'            => ['label' => 'Note Type (customer/internal)', 'type' => 'string'],
        ];
    }

    public static function getConfigFields(): array
    {
        return [
            'note_type' => [
                'label'   => 'Note Type (optional)',
                'type'    => 'select',
                'options' => [
                    ['value' => 'any',      'label' => 'Any'],
                    ['value' => 'customer', 'label' => 'Customer note'],
                    ['value' => 'internal', 'label' => 'Internal note'],
                ],
                'default' => 'any',
            ],
        ];
    }

    public function register(callable $callback): void
    {
        $this->callback = $callback;

        add_action('woocommerce_order_note_added', function (int $commentId, \WC_Order $order): void {
            if (!$order instanceof \WC_Order) {
                return;
            }

            $data = $this->handle($commentId, $order);
            $this->fire($data);
        }, 15, 2);
    }

    public function handle(mixed ...$args): array
    {
        $commentId = (int) $args[0];
        $order     = $args[1];
        $orderId   = $order->get_id();
        $dateObj   = $order->get_date_created();

        $comment     = get_comment($commentId);
        $isCustomer  = get_comment_meta($commentId, 'is_customer_note', true);
        $noteType    = ($isCustomer !== '') ? 'customer' : 'internal';

        return [
            'order_id'           => $orderId,
            'order_number'       => $order->get_order_number(),
            'order_status'       => $order->get_status(),
            'order_total'        => $order->get_total(),
            'order_currency'     => $order->get_currency(),
            'order_date'         => $dateObj ? $dateObj->format('Y-m-d H:i:s') : '',
            'billing_first_name' => $order->get_billing_first_name(),
            'billing_last_name'  => $order->get_billing_last_name(),
            'billing_email'      => $order->get_billing_email(),
            'note_id'            => $commentId,
            'note_content'       => $comment ? $comment->comment_content : '',
            'note_author'        => $comment ? $comment->comment_author : '',
            'note_date'          => $comment ? $comment->comment_date : '',
            'note_type'          => $noteType,
        ];
    }
}
