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

class DiscordNotifier extends AbstractNotifier {
	public static function type(): string        { return 'discord'; }
	public static function label(): string       { return 'Discord'; }
	public static function description(): string { return 'Post to a Discord channel via webhook'; }

	public static function config_schema(): array {
		return [
			[ 'key' => 'webhook_url', 'label' => 'Webhook URL', 'type' => 'password', 'secret' => true, 'required' => true,
			  'help' => 'Discord → Channel settings → Integrations → Webhooks → New Webhook' ],
			[ 'key' => 'username', 'label' => 'Username (optional)', 'type' => 'text', 'default' => 'BackReload' ],
		];
	}

	public function send( array $config, array $payload ): void {
		$color = $payload['level'] === 'error' ? 0xe11d48 : 0x10b981;

		$this->post_json( $config['webhook_url'], [
			'username' => $config['username'] ?? 'BackReload',
			'embeds'   => [[
				'title'       => $payload['title'],
				'description' => $payload['message'],
				'color'       => $color,
				'timestamp'   => gmdate( 'c' ),
				'fields'      => [
					[ 'name' => 'Backup ID', 'value' => $payload['context']['backup_id'] ?? 'n/a', 'inline' => true ],
					[ 'name' => 'Site',      'value' => $payload['context']['site_url']  ?? 'n/a', 'inline' => true ],
				],
			]],
		] );
	}
}
