<?php

namespace Tests\Feature\Api\V1\BroadcastEmail;

use App\Models\User;
use App\Services\EmailService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class BroadcastEmailControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_options_returns_parents_and_senders(): void
    {
        $this->seedParentData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/broadcast-email/options');

        $response->assertOk();
        $this->assertNotEmpty($response->json('parents'));
        $this->assertNotEmpty($response->json('fromOptions'));
    }

    public function test_send_test_only_dispatches_email(): void
    {
        $this->seedParentData();

        $fake = new class extends EmailService {
            public array $sent = [];

            public function send(
                array|string $to,
                string $subject,
                string $html,
                string $fromKey = 'general',
                array $cc = [],
                array $bcc = []
            ): bool
            {
                $this->sent[] = compact('to', 'subject', 'html', 'fromKey', 'cc', 'bcc');
                return true;
            }
        };

        $this->app->instance(EmailService::class, $fake);

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/broadcast-email/send', [
            'mode' => 'personalized',
            'subject' => 'Test Subject',
            'from_key' => 'general',
            'body_html' => '<p>Hello {{name}}</p>',
            'send_test_only' => true,
            'test_email' => 'parent@example.com',
            'wrap_layout' => false,
        ]);

        $response->assertOk();
        $response->assertJson([
            'ok' => true,
        ]);
        $this->assertCount(1, $fake->sent);
    }

    private function seedParentData(): void
    {
        DB::table('roles')->insert([
            'id' => 1,
            'name' => 'parent',
            'slug' => 'parent',
            'is_active' => 1,
        ]);

        DB::table('users')->insert([
            'id' => 10,
            'school_id' => 1,
            'firstname' => 'Parent',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            '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('user_roles')->insert([
            'user_id' => 10,
            'role_id' => 1,
        ]);
    }

    private function createUser(): User
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'admin@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',
        ]);

        return User::query()->findOrFail(1);
    }
}
