<?php

namespace Tests\Feature;

use App\Models\IftaQuestion;
use App\Models\IftaReply;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DarulIftaModerationTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_can_login_to_backend(): void
    {
        $admin = User::factory()->create([
            'email' => 'admin@example.test',
            'password' => 'password',
            'role' => 'admin',
            'is_active' => true,
        ]);

        $this->post(route('login.store'), [
            'email' => $admin->email,
            'password' => 'password',
        ])->assertRedirect(route('admin.dashboard'));

        $this->assertAuthenticatedAs($admin);
    }

    public function test_user_question_waits_for_admin_approval_before_publication(): void
    {
        $user = User::factory()->create(['role' => 'user', 'is_active' => true]);
        $admin = User::factory()->create(['role' => 'admin', 'is_active' => true]);

        $this->actingAs($user)->post(route('questions.store'), [
            'title' => 'Can a student request leave for family travel?',
            'category' => 'Education',
            'body' => 'A guardian needs to travel with a student for several days. What is the correct process?',
        ])->assertRedirect();

        $question = IftaQuestion::firstOrFail();

        $this->assertFalse($question->is_approved);
        $this->assertSame('pending', $question->status);

        $this->get(route('darul-ifta'))
            ->assertOk()
            ->assertDontSee($question->title);

        $this->actingAs($admin)->put(route('admin.questions.update', $question), [
            'category' => 'Education',
            'status' => 'open',
            'is_approved' => '1',
        ])->assertRedirect();

        $this->assertTrue($question->fresh()->is_approved);

        $this->get(route('darul-ifta'))
            ->assertOk()
            ->assertSee($question->title);
    }

    public function test_admin_reply_is_public_immediately_and_user_reply_needs_approval(): void
    {
        $admin = User::factory()->create(['role' => 'admin', 'is_active' => true]);
        $author = User::factory()->create(['role' => 'user', 'is_active' => true]);
        $commenter = User::factory()->create(['role' => 'user', 'is_active' => true]);

        $question = IftaQuestion::create([
            'user_id' => $author->id,
            'locale' => app()->getLocale(),
            'category' => 'Fiqh',
            'title' => 'What is the ruling on a missed class?',
            'body' => 'The student missed one day of class due to illness. What should the family do next?',
            'status' => 'open',
            'is_approved' => true,
            'approved_at' => now(),
            'approved_by' => $admin->id,
        ]);

        $this->actingAs($commenter)->post(route('questions.replies.store', $question), [
            'body' => 'Community reply waiting for moderation.',
        ])->assertRedirect();

        $userReply = IftaReply::where('body', 'Community reply waiting for moderation.')->firstOrFail();

        $this->assertFalse($userReply->is_approved);
        $this->assertFalse($userReply->is_official);

        $this->actingAs($admin)->post(route('admin.questions.replies.store', $question), [
            'body' => 'Official answer that should be visible immediately.',
        ])->assertRedirect();

        $this->assertDatabaseHas('ifta_replies', [
            'body' => 'Official answer that should be visible immediately.',
            'is_official' => true,
            'is_approved' => true,
        ]);

        $this->get(route('darul-ifta'))
            ->assertOk()
            ->assertSee('Official answer that should be visible immediately.')
            ->assertDontSee('Community reply waiting for moderation.');

        $this->actingAs($admin)->put(route('admin.replies.update', $userReply), [
            'body' => $userReply->body,
            'is_approved' => '1',
        ])->assertRedirect();

        $this->get(route('darul-ifta'))
            ->assertOk()
            ->assertSee('Community reply waiting for moderation.');
    }
}
