<?php

namespace Tests\Unit\Services\Messaging;

use App\Models\ParentModel;
use App\Models\Student;
use App\Models\User;
use App\Services\Messaging\MessageRecipientService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class MessageRecipientServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_teachers_returns_list(): void
    {
        $teacher = $this->createUser('teacher@example.com');

        DB::table('teacher_class')->insert([
            'teacher_id' => $teacher->id,
            'class_section_id' => 1,
            'position' => 'main',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new MessageRecipientService();
        $rows = $service->teachers();

        $this->assertSame($teacher->id, $rows[0]['id']);
    }

    public function test_parents_returns_list(): void
    {
        $teacher = $this->createUser('teacher2@example.com');
        $parent = $this->createUser('parent@example.com');
        $secondParent = $this->createUser('parent2@example.com');

        DB::table('teacher_class')->insert([
            'teacher_id' => $teacher->id,
            'class_section_id' => 2,
            'position' => 'main',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $studentId = Student::query()->insertGetId([
            'school_id' => 'S-1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 10,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => $parent->id,
            'year_of_registration' => '2025',
            'is_active' => 1,
            'is_new' => 1,
        ]);

        DB::table('student_class')->insert([
            'student_id' => $studentId,
            'class_section_id' => 2,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        ParentModel::query()->create([
            'secondparent_firstname' => 'Second',
            'secondparent_lastname' => 'Parent',
            'secondparent_email' => 'parent2@example.com',
            'secondparent_phone' => '5555555555',
            'firstparent_id' => $parent->id,
            'secondparent_id' => $secondParent->id,
            'school_year' => '2025-2026',
        ]);

        $service = new MessageRecipientService();
        $rows = $service->parents();

        $ids = array_column($rows, 'id');
        $this->assertContains($parent->id, $ids);
        $this->assertContains($secondParent->id, $ids);
    }

    private function createUser(string $email): User
    {
        return User::query()->create([
            'firstname' => 'Test',
            'lastname' => 'User',
            'email' => $email,
            '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',
        ]);
    }
}
