<?php

namespace Tests\Feature\Api\V1\Whatsapp;

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

class WhatsappLinkControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_paginated_links(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        DB::table('whatsapp_group_links')->insert([
            'class_section_id' => 200,
            'class_section_name' => 'Grade 1',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'invite_link' => 'https://chat.whatsapp.com/abc',
            'active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->getJson('/api/v1/whatsapp/links');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertNotEmpty($response->json('data.links'));
        $this->assertArrayHasKey('class_section_id', $response->json('data.links.0'));
    }

    public function test_show_returns_single_link(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $linkId = DB::table('whatsapp_group_links')->insertGetId([
            'class_section_id' => 201,
            'class_section_name' => 'Grade 2',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'invite_link' => 'https://chat.whatsapp.com/xyz',
            'active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->getJson('/api/v1/whatsapp/links/' . $linkId);

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $response->assertJsonPath('data.link.id', $linkId);
    }

    public function test_store_creates_link(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $payload = [
            'class_section_id' => 202,
            'class_section_name' => 'Grade 3',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'invite_link' => 'https://chat.whatsapp.com/new',
            'active' => true,
        ];

        $response = $this->postJson('/api/v1/whatsapp/links', $payload);

        $response->assertStatus(201);
        $response->assertJsonPath('status', true);
        $this->assertDatabaseHas('whatsapp_group_links', [
            'class_section_id' => 202,
            'invite_link' => 'https://chat.whatsapp.com/new',
        ]);
    }

    public function test_update_updates_link(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $linkId = DB::table('whatsapp_group_links')->insertGetId([
            'class_section_id' => 203,
            'class_section_name' => 'Grade 4',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'invite_link' => 'https://chat.whatsapp.com/old',
            'active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->patchJson('/api/v1/whatsapp/links/' . $linkId, [
            'invite_link' => 'https://chat.whatsapp.com/updated',
            'active' => false,
        ]);

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertDatabaseHas('whatsapp_group_links', [
            'id' => $linkId,
            'invite_link' => 'https://chat.whatsapp.com/updated',
            'active' => 0,
        ]);
    }

    public function test_destroy_deletes_link(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $linkId = DB::table('whatsapp_group_links')->insertGetId([
            'class_section_id' => 204,
            'class_section_name' => 'Grade 5',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'invite_link' => 'https://chat.whatsapp.com/delete',
            'active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $response = $this->deleteJson('/api/v1/whatsapp/links/' . $linkId);

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertDatabaseMissing('whatsapp_group_links', ['id' => $linkId]);
    }

    public function test_store_validation_rejects_invalid_payload(): void
    {
        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/whatsapp/links', [
            'class_section_id' => null,
        ]);

        $response->assertStatus(422);
        $response->assertJsonPath('message', 'Validation failed.');
    }

    public function test_requires_authentication(): void
    {
        $response = $this->getJson('/api/v1/whatsapp/links');

        $response->assertStatus(401);
    }

    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);
    }
}
