<?php

namespace Tests\Unit\Services\Parents;

use App\Services\Parents\ParentConfigService;
use App\Services\Parents\ParentEmergencyContactService;
use App\Services\PhoneFormatterService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ParentEmergencyContactServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_store_creates_contact(): void
    {
        $this->seedConfig();
        $parentId = $this->seedParent();

        $service = new ParentEmergencyContactService(new ParentConfigService(), new PhoneFormatterService());
        $contact = $service->store($parentId, [
            'name' => 'Sam Contact',
            'cellphone' => '1234567890',
            'email' => 'sam@example.com',
            'relation' => 'Uncle',
        ]);

        $this->assertSame('Sam Contact', $contact['emergency_contact_name']);
        $this->assertDatabaseHas('emergency_contacts', ['parent_id' => $parentId, 'emergency_contact_name' => 'Sam Contact']);
    }

    private function seedParent(): int
    {
        return DB::table('users')->insertGetId([
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '1234567890',
            'email' => 'parent5@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);
    }

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