<?php

namespace Tests\Feature;

use App\Models\Product;
use App\Models\ProductReview;
use App\Models\User;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class ReviewApiTest extends TestCase
{
    use RefreshDatabase;

    public function test_can_get_paginated_reviews_for_product()
    {
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        $user = User::factory()->create();
        
        ProductReview::factory()->count(15)->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
            'is_approved' => true,
        ]);

        $response = $this->getJson("/api/products/{$product->id}/reviews");

        $response->assertStatus(200)
                 ->assertJsonStructure([
                     'success',
                     'data',
                     'meta' => [
                         'current_page',
                         'last_page',
                         'per_page',
                         'total',
                     ],
                 ]);
        
        $this->assertCount(10, $response->json('data')); // Default per_page
    }

    public function test_only_approved_reviews_are_returned()
    {
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        $user = User::factory()->create();
        
        ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
            'is_approved' => true,
        ]);
        
        ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
            'is_approved' => false,
        ]);

        $response = $this->getJson("/api/products/{$product->id}/reviews");

        $response->assertStatus(200);
        $this->assertCount(1, $response->json('data'));
        $this->assertTrue($response->json('data.0.is_approved'));
    }

    public function test_can_mark_review_as_helpful()
    {
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        $user = User::factory()->create();
        
        $review = ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
            'is_approved' => true,
            'helpful_count' => 5,
        ]);

        $response = $this->postJson("/api/reviews/{$review->id}/helpful");

        $response->assertStatus(200)
                 ->assertJson([
                     'success' => true,
                     'data' => [
                         'helpful_count' => 6,
                     ],
                 ]);
        
        $review->refresh();
        $this->assertEquals(6, $review->helpful_count);
    }

    public function test_unauthenticated_user_cannot_create_review_when_guest_reviews_disabled()
    {
        config(['reviews.allow_guest_reviews' => false]);
        
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);

        $response = $this->postJson("/api/products/{$product->id}/reviews", [
            'rating' => 5,
        ]);

        $response->assertStatus(401);
    }
}

