<?php

namespace Tests\Feature;

use App\Models\PageContent;
use App\Models\PaymentMethod;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminPlatformFeaturesTest extends TestCase
{
    use RefreshDatabase;

    public function test_payment_method_crud_is_exposed_to_the_api(): void
    {
        $admin = User::factory()->create(['role' => 'admin']);

        $this->actingAs($admin)->post(route('admin.payment-methods.store'), [
            'name' => 'Cash on Delivery',
            'code' => 'cod',
            'description' => 'Pay the courier.',
            'instructions' => 'Prepare exact cash.',
            'sort_order' => 2,
            'is_active' => 1,
        ])->assertRedirect(route('admin.payment-methods.index'));

        $this->getJson('/api/v1/payment-methods')
            ->assertOk()
            ->assertJsonPath('data.0.code', 'cod')
            ->assertJsonPath('data.0.name', 'Cash on Delivery');
    }

    public function test_module_permissions_protect_navigation_routes(): void
    {
        $admin = User::factory()->create([
            'role' => 'admin',
            'permissions' => ['dashboard'],
        ]);

        $this->actingAs($admin)->get(route('admin.dashboard'))->assertOk();
        $this->actingAs($admin)->get(route('admin.products.index'))->assertForbidden();
    }

    public function test_homepage_api_returns_all_configured_home_sections(): void
    {
        PageContent::create(['section' => 'home', 'title_en' => 'Home']);
        PageContent::create(['section' => 'home_testimonials', 'title_en' => 'Customer Stories']);

        $this->getJson('/api/v1/pages/homepage')
            ->assertOk()
            ->assertJsonPath('data.home.title', 'Home')
            ->assertJsonPath('data.sections.testimonials.title', 'Customer Stories');
    }
}
