<?php

namespace Tests\Feature;

use App\Models\Product;
use App\Models\ProductReview;
use App\Models\User;
use App\Models\Category;
use App\Models\Order;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class ReviewCreationTest extends TestCase
{
    use RefreshDatabase;

    public function test_authenticated_user_can_create_review()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        Sanctum::actingAs($user);

        $response = $this->postJson("/api/products/{$product->id}/reviews", [
            'rating' => 5,
            'title' => 'Great product!',
            'body' => 'I really enjoyed this product. Highly recommended!',
        ]);

        $response->assertStatus(201)
                 ->assertJson(['success' => true]);
        
        $this->assertDatabaseHas('product_reviews', [
            'product_id' => $product->id,
            'user_id' => $user->id,
            'rating' => 5,
            'title' => 'Great product!',
        ]);
    }

    public function test_review_requires_rating()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        Sanctum::actingAs($user);

        $response = $this->postJson("/api/products/{$product->id}/reviews", [
            'title' => 'Great product!',
            'body' => 'I really enjoyed this product.',
        ]);

        $response->assertStatus(422)
                 ->assertJsonValidationErrors(['rating']);
    }

    public function test_rating_must_be_between_1_and_5()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        Sanctum::actingAs($user);

        $response = $this->postJson("/api/products/{$product->id}/reviews", [
            'rating' => 6,
        ]);

        $response->assertStatus(422)
                 ->assertJsonValidationErrors(['rating']);
    }

    public function test_user_cannot_review_same_product_twice_within_rate_limit()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        Sanctum::actingAs($user);

        // Create first review
        ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
            'created_at' => now()->subDays(10), // Within 30 day limit
        ]);

        // Try to create second review
        $response = $this->postJson("/api/products/{$product->id}/reviews", [
            'rating' => 4,
        ]);

        $response->assertStatus(429);
    }

    public function test_review_auto_approved_when_config_disabled()
    {
        config(['reviews.require_approval' => false]);
        
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        Sanctum::actingAs($user);

        $response = $this->postJson("/api/products/{$product->id}/reviews", [
            'rating' => 5,
        ]);

        $response->assertStatus(201);
        
        $this->assertDatabaseHas('product_reviews', [
            'product_id' => $product->id,
            'user_id' => $user->id,
            'is_approved' => true,
        ]);
    }

    public function test_review_pending_when_approval_required()
    {
        config(['reviews.require_approval' => true]);
        
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        Sanctum::actingAs($user);

        $response = $this->postJson("/api/products/{$product->id}/reviews", [
            'rating' => 5,
        ]);

        $response->assertStatus(201);
        
        $this->assertDatabaseHas('product_reviews', [
            'product_id' => $product->id,
            'user_id' => $user->id,
            'is_approved' => false,
        ]);
    }
}

