<?php

namespace Tests\Feature\Integration;

use App\Models\Club;
use App\Models\Student;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Feature\Integration\Concerns\CreatesSchoolFixtures;
use Tests\TestCase;

class ClubDiscussionTest extends TestCase
{
    use CreatesSchoolFixtures;
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(RolesAndPermissionsSeeder::class);
    }

    public function test_admin_can_create_club_and_assign_student(): void
    {
        $admin = $this->createAdministrator();
        [$year] = $this->createAcademicYearWithTerm();
        $staff = $this->createStaff();
        $student = $this->createStudent();

        $this->actingAs($admin)
            ->post(route('school.clubs.store'), [
                'name' => 'Science Club',
                'code' => 'CLB-SCI',
                'category' => 'academic',
                'patron_id' => $staff->id,
                'academic_year_id' => $year->id,
                'discussion_enabled' => '1',
                'status' => 'active',
            ])
            ->assertRedirect();

        $club = Club::where('code', 'CLB-SCI')->firstOrFail();

        $this->actingAs($admin)
            ->post(route('school.clubs.assign', $club), [
                'student_ids' => [$student->id],
                'role' => 'member',
            ])
            ->assertRedirect();

        $this->assertTrue($club->fresh()->students->contains($student));
    }

    public function test_student_can_access_club_discussion_when_member(): void
    {
        $staff = $this->createStaff();
        [$year] = $this->createAcademicYearWithTerm();
        $student = $this->createStudent();
        $user = User::factory()->create(['student_id' => $student->id]);
        $user->assignRole('Student');

        $club = Club::query()->create([
            'name' => 'Drama Club',
            'code' => 'CLB-DRA',
            'patron_id' => $staff->id,
            'academic_year_id' => $year->id,
            'discussion_enabled' => true,
            'status' => 'active',
        ]);

        $club->students()->attach($student->id, ['role' => 'member', 'joined_on' => now(), 'status' => 'active']);

        $this->actingAs($user)
            ->get(route('student.clubs.show', $club))
            ->assertOk()
            ->assertSee('Drama Club');

        $this->actingAs($user)
            ->post(route('student.clubs.messages.store', $club), [
                'body' => 'Hello club! 👋',
            ])
            ->assertRedirect();

        $this->assertDatabaseHas('club_messages', [
            'club_id' => $club->id,
            'user_id' => $user->id,
        ]);
    }

    public function test_non_member_cannot_access_club_discussion(): void
    {
        $staff = $this->createStaff();
        [$year] = $this->createAcademicYearWithTerm();
        $student = $this->createStudent();
        $user = User::factory()->create(['student_id' => $student->id]);
        $user->assignRole('Student');

        $club = Club::query()->create([
            'name' => 'Chess Club',
            'code' => 'CLB-CHS',
            'patron_id' => $staff->id,
            'academic_year_id' => $year->id,
            'discussion_enabled' => true,
            'status' => 'active',
        ]);

        $this->actingAs($user)
            ->get(route('student.clubs.show', $club))
            ->assertForbidden();
    }
}
