<?php

namespace Tests\Unit\Services\Whatsapp;

use App\Services\Whatsapp\WhatsappInviteBundleService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class WhatsappInviteBundleServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_bundle_for_parent_returns_links_and_emails(): void
    {
        DB::table('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'One',
            'cellphone' => '5555555555',
            'email' => 'parent1@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('parents')->insert([
            'secondparent_firstname' => 'Parent',
            'secondparent_lastname' => 'Two',
            'secondparent_email' => 'parent2@example.com',
            'secondparent_phone' => '5555555555',
            'firstparent_id' => 10,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('students')->insert([
            'id' => 100,
            'school_id' => 1,
            'firstname' => 'Student',
            'lastname' => 'One',
            'parent_id' => 10,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('student_class')->insert([
            'student_id' => 100,
            'class_section_id' => 200,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $linkBySection = [
            200 => [
                'class_section_id' => 200,
                'class_section_name' => 'Grade 1',
                'invite_link' => 'https://chat.whatsapp.com/abc',
            ],
        ];

        $service = new WhatsappInviteBundleService();
        $bundle = $service->bundleForParent(10, $linkBySection, '2025-2026', 'Fall');

        $this->assertNotNull($bundle);
        $this->assertNotEmpty($bundle['emails']);
        $this->assertNotEmpty($bundle['links']);
    }

    public function test_consolidate_bundles_merges_by_parent(): void
    {
        $service = new WhatsappInviteBundleService();

        $merged = $service->consolidateBundles([
            [
                'parent' => ['id' => 10],
                'emails' => ['a@example.com'],
                'sections' => [['class_section_id' => 1]],
                'links' => ['https://chat.whatsapp.com/a'],
            ],
            [
                'parent' => ['id' => 10],
                'emails' => ['b@example.com'],
                'sections' => [['class_section_id' => 2]],
                'links' => ['https://chat.whatsapp.com/b'],
            ],
        ]);

        $this->assertCount(1, $merged);
        $this->assertCount(2, $merged[0]['emails']);
    }
}
