<?php

namespace Tests\Unit\Services\Notifications;

use App\Models\Notification;
use App\Models\UserNotification;
use App\Services\Notifications\NotificationDispatchService;
use App\Services\Notifications\NotificationRecipientService;
use App\Services\Notifications\NotificationSendService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class NotificationSendServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_send_creates_notification_and_user_notifications(): void
    {
        $recipients = Mockery::mock(NotificationRecipientService::class);
        $recipients->shouldReceive('getRecipients')
            ->with('parent')
            ->andReturn([
                ['id' => 10, 'email' => 'parent1@example.com', 'phone' => '5551112222'],
                ['id' => 11, 'email' => 'parent2@example.com', 'phone' => null],
            ]);

        $dispatcher = Mockery::mock(NotificationDispatchService::class);
        $dispatcher->shouldReceive('sendEmail')->once();
        $dispatcher->shouldReceive('sendSms')->once();

        $service = new NotificationSendService($recipients, $dispatcher);

        $result = $service->send([
            'title' => 'Announcement',
            'message' => 'Hello parents',
            'target_group' => 'parent',
            'channels' => ['in_app', 'email', 'sms'],
        ], 99);

        $this->assertTrue($result['ok']);
        $this->assertSame(2, $result['recipient_count']);
        $this->assertDatabaseHas('notifications', [
            'title' => 'Announcement',
            'target_group' => 'parent',
        ]);
        $this->assertSame(1, Notification::query()->count());
        $this->assertSame(2, UserNotification::query()->count());
    }
}
