<?php

namespace Tests\Unit\Services\Email;

use App\Services\Email\EmailExtractorService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class EmailExtractorServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_and_compare_emails(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'User',
            'lastname' => 'One',
            'cellphone' => '5555555555',
            'email' => 'user1@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',
        ]);

        DB::table('parents')->insert([
            'secondparent_firstname' => 'Parent',
            'secondparent_lastname' => 'Two',
            'secondparent_email' => 'parent2@example.com',
            'secondparent_phone' => '5555555555',
            'firstparent_id' => 1,
            'secondparent_id' => 2,
            'school_year' => '2025-2026',
        ]);

        $service = new EmailExtractorService();
        $emails = $service->listEmails();

        $this->assertContains('user1@example.com', $emails['users']);
        $this->assertContains('parent2@example.com', $emails['parents']);

        $result = $service->compare(['user1@example.com', 'missing@example.com']);

        $this->assertContains('user1@example.com', $result['existed']);
        $this->assertContains('parent2@example.com', $result['needToAdd']);
    }
}
