<?php

namespace Tests\Unit\Services\Whatsapp;

use App\Events\WhatsappInvitesSend;
use App\Services\Whatsapp\WhatsappContextService;
use App\Services\Whatsapp\WhatsappInviteBundleService;
use App\Services\Whatsapp\WhatsappInviteNotificationService;
use App\Services\Whatsapp\WhatsappInviteService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

class WhatsappInviteServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_send_returns_success_for_parent_mode(): void
    {
        $this->seedInviteData();
        Event::listen(WhatsappInvitesSend::class, static function () {
        });

        $service = new WhatsappInviteService(
            new WhatsappContextService(),
            new WhatsappInviteBundleService(),
            new WhatsappInviteNotificationService()
        );

        $result = $service->send([
            'mode' => 'parents',
            'parent_ids' => [10],
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertGreaterThanOrEqual(1, $result['triggered']);
    }

    private function seedInviteData(): 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(),
        ]);

        DB::table('whatsapp_group_links')->insert([
            'class_section_id' => 200,
            'class_section_name' => 'Grade 1',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'invite_link' => 'https://chat.whatsapp.com/abc',
            'active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
