<?php
namespace Backreload\Notifiers;
if ( ! defined( 'ABSPATH' ) ) { exit; }

class EmailNotifier extends AbstractNotifier {
	public static function type(): string        { return 'email'; }
	public static function label(): string       { return 'Email'; }
	public static function description(): string { return 'Send via WordPress wp_mail()'; }

	public static function config_schema(): array {
		return [
			[ 'key' => 'to', 'label' => 'Recipient email', 'type' => 'text', 'required' => true, 'default' => get_option( 'admin_email' ) ],
		];
	}

	public function validate( array $config ): array {
		$config = parent::validate( $config );
		if ( ! is_email( $config['to'] ?? '' ) ) {
			throw new NotifierValidationException( [ 'to' => 'Enter a valid email address' ] );
		}
		return $config;
	}

	public function send( array $config, array $payload ): void {
		$ok = wp_mail( $config['to'], $payload['title'], $payload['message'] );
		if ( ! $ok ) {
			throw new \RuntimeException( esc_html( 'wp_mail() returned false' ) );
		}
	}
}
