<?php
/**
 * Email notifications (Free tier: email only).
 */

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

class CPECS_Notifications {

	/** @var CPECS_Settings */
	private $settings;

	public function __construct( CPECS_Settings $settings ) {
		$this->settings = $settings;
	}

	public function notify_new_visitor_message( $conversation_id, $message ) {
		$to = $this->settings->get( 'notify_email' );
		if ( empty( $to ) ) {
			return;
		}

		$conversation = cpecs()->db->get_conversation( $conversation_id );
		$visitor_name = $conversation && $conversation->visitor_name ? $conversation->visitor_name : __( 'A visitor', 'capripio-easy-chat-support' );

		$subject = sprintf(
			/* translators: %s: visitor name */
			__( '[Live Chat] New message from %s', 'capripio-easy-chat-support' ),
			$visitor_name
		);

		$inbox_url = admin_url( 'admin.php?page=cpecs-inbox&conversation=' . (int) $conversation_id );

		$body  = $visitor_name . " " . __( 'sent a new message:', 'capripio-easy-chat-support' ) . "\n\n";
		$body .= $message . "\n\n";
		$body .= __( 'Reply from your dashboard:', 'capripio-easy-chat-support' ) . ' ' . $inbox_url;

		wp_mail( $to, $subject, $body );
	}
}
