<?php

namespace Tests\Unit\Services\BroadcastEmail;

use App\Services\BroadcastEmail\BroadcastEmailRecipientService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class BroadcastEmailRecipientServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_parents_with_emails_filters_missing_email(): void
    {
        $this->seedParentData();

        $service = new BroadcastEmailRecipientService();
        $parents = $service->parentsWithEmails();

        $this->assertCount(1, $parents);
        $this->assertSame('parent@example.com', $parents[0]['email']);
    }

    public function test_recipients_by_ids_returns_names(): void
    {
        $this->seedParentData();

        $service = new BroadcastEmailRecipientService();
        $recipients = $service->recipientsByIds([10]);

        $this->assertCount(1, $recipients);
        $this->assertSame('Parent User', $recipients[0]['name']);
    }

    private function seedParentData(): void
    {
        DB::table('roles')->insert([
            'id' => 1,
            'name' => 'parent',
            'slug' => 'parent',
            'is_active' => 1,
        ]);

        DB::table('users')->insert([
            [
                'id' => 10,
                'school_id' => 1,
                'firstname' => 'Parent',
                'lastname' => 'User',
                'cellphone' => '5555555555',
                'email' => 'parent@example.com',
                'address_street' => '123 Main',
                'city' => 'City',
                'state' => 'ST',
                'zip' => '12345',
                'accept_school_policy' => 1,
                'is_verified' => 1,
                'status' => 'Active',
                'is_suspended' => 0,
                'failed_attempts' => 0,
                'password' => bcrypt('secret'),
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 11,
                'school_id' => 1,
                'firstname' => 'No',
                'lastname' => 'Email',
                'cellphone' => '5555555555',
                'email' => '',
                'address_street' => '123 Main',
                'city' => 'City',
                'state' => 'ST',
                'zip' => '12345',
                'accept_school_policy' => 1,
                'is_verified' => 1,
                'status' => 'Active',
                'is_suspended' => 0,
                'failed_attempts' => 0,
                'password' => bcrypt('secret'),
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);

        DB::table('user_roles')->insert([
            ['user_id' => 10, 'role_id' => 1],
            ['user_id' => 11, 'role_id' => 1],
        ]);
    }
}
