<?php

namespace Tests\Unit\Services\Users;

use App\Models\User;
use App\Services\Users\UserListService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class UserListServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_includes_roles_and_second_parent_flag(): void
    {
        DB::table('roles')->insert([
            'id' => 5,
            'name' => 'Parent',
            'slug' => 'parent',
            'description' => 'Parent role',
            'dashboard_route' => 'parent/dashboard',
            'priority' => 1,
            'is_active' => 1,
        ]);

        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Jane',
            'lastname' => 'Doe',
            'cellphone' => '5551112222',
            'email' => 'second.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('user_roles')->insert([
            'user_id' => 1,
            'role_id' => 5,
        ]);

        DB::table('parents')->insert([
            'secondparent_firstname' => 'Jane',
            'secondparent_lastname' => 'Doe',
            'secondparent_gender' => 'Female',
            'secondparent_email' => 'second.parent@example.com',
            'secondparent_phone' => '5551112222',
            'firstparent_id' => 10,
            'secondparent_id' => 11,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $service = app(UserListService::class);
        $rows = $service->list('id', 'asc');

        $this->assertCount(1, $rows);
        $this->assertSame('second.parent@example.com', $rows[0]['user']['email']);
        $this->assertSame(['Parent'], $rows[0]['roles']);
        $this->assertSame('Yes', $rows[0]['second_from_parents']);
    }
}
