<?php

namespace Tests\Unit\Services\Grading;

use App\Services\EmailService;
use App\Services\Grading\BelowSixtyEmailService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class BelowSixtyEmailServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_send_requires_student_id(): void
    {
        $service = new BelowSixtyEmailService(Mockery::mock(EmailService::class));
        $result = $service->send([]);

        $this->assertFalse($result['ok']);
    }

    public function test_send_delivers_to_guardians(): void
    {
        DB::table('families')->insert([
            'id' => 1,
            'family_code' => 'FAM-1',
            'household_name' => 'Family',
            'is_active' => 1,
        ]);

        DB::table('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '5551112222',
            'email' => 'parent@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('family_guardians')->insert([
            'family_id' => 1,
            'user_id' => 10,
            'relation' => 'primary',
            'is_primary' => 1,
            'receive_emails' => 1,
            'receive_sms' => 0,
        ]);

        DB::table('students')->insert([
            'id' => 22,
            'school_id' => 1,
            'parent_id' => 10,
            'firstname' => 'Student',
            'lastname' => 'One',
            'dob' => '2015-01-01',
            'age' => 10,
            'gender' => 'Male',
            'is_active' => 1,
            'photo_consent' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
        ]);

        DB::table('family_students')->insert([
            'family_id' => 1,
            'student_id' => 22,
            'is_primary_home' => 1,
        ]);

        $email = Mockery::mock(EmailService::class);
        $email->shouldReceive('send')->once()->andReturn(true);

        $service = new BelowSixtyEmailService($email);
        $result = $service->send([
            'student_id' => 22,
            'student_name' => 'Student',
            'class_section_name' => 'Class',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'scores' => ['semester_score' => 55],
            'comment' => 'Needs improvement',
            'html' => '<p>Report</p>',
        ]);

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