<?php

namespace Tests\Unit\Services\Preferences;

use App\Services\Preferences\PreferencesQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class PreferencesQueryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_get_for_user_returns_defaults_when_missing(): void
    {
        $service = app(PreferencesQueryService::class);
        $payload = $service->getForUser(1);

        $this->assertTrue($payload['preferences']['receive_email_notifications']);
        $this->assertSame('en', $payload['preferences']['language']);
    }

    public function test_paginate_filters_by_user_id(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'firstname' => 'Test',
            'lastname' => 'User',
            'email' => 'test@example.com',
            'cellphone' => '5555555555',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'status' => 'Active',
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('user_preferences')->insert([
            'user_id' => 1,
            'notification_email' => 1,
            'notification_sms' => 1,
        ]);

        $service = app(PreferencesQueryService::class);
        $pager = $service->paginate(['user_id' => 1], 1, 10);

        $this->assertSame(1, $pager->total());
        $this->assertSame(1, $pager->items()[0]->user_id);
    }
}
