<?php

namespace App\Services;

use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;

class FcmService
{
	protected $messaging;
	
	public function __construct(Messaging $messaging)
	{
		$this->messaging = $messaging;
	}
	
	public function sendNotification($token, $title, $body)
	{
		$message = CloudMessage::withTarget('token', $token)
			->withNotification(Notification::create($title, $body))
			->withData(['click_action' => 'FLUTTER_NOTIFICATION_CLICK']);
		
		try {
			$this->messaging->send($message);
			return true;
		} catch (\Exception $e) {
			\Log::error('FCM send failed: ' . $e->getMessage());
			return false;
		}
	}
}