<?php

namespace Tests\Unit\Services\Whatsapp;

use App\Services\Whatsapp\WhatsappMembershipService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;

class WhatsappMembershipServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_update_membership_saves_primary(): void
    {
        $service = new WhatsappMembershipService();

        $result = $service->updateMembership([
            'class_section_id' => 200,
            'primary_id' => 10,
            'primary_member' => true,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ], 1);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('whatsapp_group_memberships', [
            'class_section_id' => 200,
            'subject_type' => 'primary',
            'subject_id' => 10,
            'is_member' => 1,
        ]);
    }

    public function test_update_membership_returns_error_when_table_missing(): void
    {
        Schema::shouldReceive('hasTable')->with('whatsapp_group_memberships')->andReturn(false);

        $service = new WhatsappMembershipService();
        $result = $service->updateMembership([
            'class_section_id' => 200,
            'primary_id' => 10,
            'primary_member' => true,
        ]);

        $this->assertFalse($result['ok']);
        $this->assertSame('Membership table missing. Please run migrations.', $result['message']);
    }
}
