<?php

namespace Tests\Unit\Services\School;

use App\Services\EmailService;
use App\Services\Notifications\UserNotificationDispatchService;
use App\Services\School\AccountEventService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class AccountEventServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_new_account_added_creates_family_links(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '5551112222',
            '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',
        ]);

        DB::table('students')->insert([
            'id' => 5,
            'school_id' => 1,
            'parent_id' => 1,
            'firstname' => 'Student',
            'lastname' => 'One',
            'dob' => '2015-01-01',
            'age' => 10,
            'gender' => 'Male',
            'is_active' => 1,
            'photo_consent' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
        ]);

        $email = Mockery::mock(EmailService::class);
        $email->shouldReceive('send')->twice()->andReturn(true);

        $notifier = Mockery::mock(UserNotificationDispatchService::class);

        $service = new AccountEventService($email, $notifier);
        $result = $service->newAccountAdded([
            'id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'email' => 'parent@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'cellphone' => '5551112222',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('families', [
            'family_code' => 'FAM-1',
        ]);
        $this->assertDatabaseHas('family_guardians', [
            'user_id' => 1,
            'is_primary' => 1,
        ]);
        $this->assertDatabaseHas('family_students', [
            'student_id' => 5,
        ]);
    }

    public function test_delete_unverified_user_handles_missing_admin_emails(): void
    {
        $email = Mockery::mock(EmailService::class);
        $email->shouldReceive('send')->andReturn(true);

        $notifier = Mockery::mock(UserNotificationDispatchService::class);
        $notifier->shouldReceive('notifyUser')->once();

        $service = new AccountEventService($email, $notifier);
        $result = $service->deleteUnverifiedUser([
            'id' => 2,
            'email' => 'inactive@example.com',
        ]);

        $this->assertTrue($result['ok']);
    }
}
