<?php

namespace VendorTasksDokanClickUp\VendorIntegration;

use VendorTasksDokanClickUp\Settings\PluginSettings;
use VendorTasksDokanClickUp\VendorIntegration\TriggerAction;

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

/**
 * Vendor Status Change Triggers
 *
 * Updates the vendor's ClickUp task when their selling status changes.
 * Adds a timestamped comment and optionally sets a configured task status.
 *
 * @since 1.5.0
 * @package VendorTasksDokanClickUp
 * @subpackage VendorIntegration
 */
class StatusTriggers {

	/**
	 * Class constructor
	 */
	public function __construct() {
		add_action( 'dokan_vendor_enabled', array( $this, 'on_vendor_enabled' ), 10, 1 );
		add_action( 'dokan_vendor_disabled', array( $this, 'on_vendor_disabled' ), 10, 1 );
	}

	/**
	 * Handle vendor approved / enabled.
	 *
	 * Fires from Vendor::make_active() when admin enables selling for a vendor.
	 *
	 * @since 1.5.0
	 *
	 * @param int $vendor_id Vendor user ID.
	 */
	public function on_vendor_enabled( int $vendor_id ): void {
		$ps = new PluginSettings();
		if ( ! $ps->is_trigger_vendor_approved() ) {
			return;
		}

		$this->update_task(
			$vendor_id,
			__( 'Vendor account approved and selling enabled.', 'alamin-vendor-tasks-dokan-clickup' ),
			$ps->get_trigger_vendor_approved_status()
		);
	}

	/**
	 * Handle vendor disabled / suspended.
	 *
	 * Fires from Vendor::make_inactive() when admin disables selling for a vendor.
	 *
	 * @since 1.5.0
	 *
	 * @param int $vendor_id Vendor user ID.
	 */
	public function on_vendor_disabled( int $vendor_id ): void {
		$ps = new PluginSettings();
		if ( ! $ps->is_trigger_vendor_disabled() ) {
			return;
		}

		$this->update_task(
			$vendor_id,
			__( 'Vendor account disabled / selling suspended.', 'alamin-vendor-tasks-dokan-clickup' ),
			$ps->get_trigger_vendor_disabled_status()
		);
	}

	/**
	 * Add a timestamped comment to the vendor's ClickUp task and optionally change its status.
	 *
	 * @since 1.5.0
	 *
	 * @param int    $vendor_id   Vendor user ID.
	 * @param string $message     Comment text (no timestamp — appended here).
	 * @param string $task_status ClickUp status to set; empty string = no change.
	 */
	private function update_task( int $vendor_id, string $message, string $task_status ): void {
		$trigger = new TriggerAction();

		$comment = sprintf(
			/* translators: 1: event description, 2: date, 3: time */
			'%1$s (%2$s %3$s)',
			$message,
			wp_date( get_option( 'date_format' ) ),
			wp_date( get_option( 'time_format' ) )
		);

		$trigger->run_if_task_exists(
			$vendor_id,
			fn( $client, $task_id ) => $client->add_task_comment( $task_id, $comment ),
			sprintf( 'StatusTriggers: failed to add comment for vendor ID %d', $vendor_id )
		);

		if ( ! empty( $task_status ) ) {
			$status = sanitize_text_field( $task_status );
			$trigger->run_if_task_exists(
				$vendor_id,
				fn( $client, $task_id ) => $client->update_task( $task_id, array( 'status' => $status ) ),
				sprintf( 'StatusTriggers: failed to set status "%s" for vendor ID %d', $status, $vendor_id )
			);
		}
	}
}
