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

class SlackNotifier extends AbstractNotifier {
	public static function type(): string        { return 'slack'; }
	public static function label(): string       { return 'Slack'; }
	public static function description(): string { return 'Post to a Slack channel via Incoming Webhook'; }

	public static function config_schema(): array {
		return [
			[ 'key' => 'webhook_url', 'label' => 'Webhook URL', 'type' => 'password', 'secret' => true, 'required' => true,
			  'help' => 'Slack → Apps → Incoming Webhooks → New Webhook (https://hooks.slack.com/services/…)' ],
			[ 'key' => 'username',    'label' => 'Username (optional)', 'type' => 'text', 'default' => 'BackReload' ],
		];
	}

	public function send( array $config, array $payload ): void {
		$color = $payload['level'] === 'error' ? '#e11d48' : '#10b981';

		$this->post_json( $config['webhook_url'], [
			'username'    => $config['username'] ?? 'BackReload',
			'icon_emoji'  => $payload['level'] === 'error' ? ':rotating_light:' : ':white_check_mark:',
			'attachments' => [[
				'color'  => $color,
				'title'  => $payload['title'],
				'text'   => $payload['message'],
				'fields' => [
					[ 'title' => 'Backup ID', 'value' => $payload['context']['backup_id'] ?? '', 'short' => true ],
					[ 'title' => 'Site',      'value' => $payload['context']['site_url']  ?? '', 'short' => true ],
				],
				'ts'     => time(),
			]],
		] );
	}
}
