<?php

namespace Tests\Unit\Services\Support;

use App\Models\User;
use App\Services\Email\EmailDispatchService;
use App\Services\Support\SupportRequestService;
use App\Services\System\GlobalConfigService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class SupportRequestServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_persists_request(): void
    {
        DB::table('configuration')->insert([
            ['config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        $user = User::query()->create([
            'firstname' => 'Test',
            'lastname' => 'User',
            'email' => 'supporter@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',
        ]);

        $mailer = $this->createMock(EmailDispatchService::class);
        $mailer->method('send')->willReturn(false);

        $service = new SupportRequestService(new GlobalConfigService(), $mailer);
        $result = $service->create($user->id, [
            'subject' => 'Help',
            'message' => 'Need assistance',
        ]);

        $this->assertNotNull($result['record']);
        $this->assertDatabaseHas('support_requests', [
            'user_id' => $user->id,
            'subject' => 'Help',
        ]);
    }
}
