<?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 ReviewManagementTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_update_own_review()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        $review = ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
            'rating' => 3,
            'title' => 'Old title',
            'created_at' => now()->subHours(12), // Within 24 hour limit
        ]);
        
        Sanctum::actingAs($user);

        $response = $this->putJson("/api/reviews/{$review->id}", [
            'rating' => 5,
            'title' => 'Updated title',
            'body' => 'Updated body',
        ]);

        $response->assertStatus(200)
                 ->assertJson(['success' => true]);
        
        $this->assertDatabaseHas('product_reviews', [
            'id' => $review->id,
            'rating' => 5,
            'title' => 'Updated title',
        ]);
    }

    public function test_user_cannot_update_other_users_review()
    {
        $user1 = User::factory()->create();
        $user2 = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        $review = ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user1->id,
        ]);
        
        Sanctum::actingAs($user2);

        $response = $this->putJson("/api/reviews/{$review->id}", [
            'rating' => 5,
        ]);

        $response->assertStatus(403);
    }

    public function test_user_can_delete_own_review()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        $review = ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
        ]);
        
        Sanctum::actingAs($user);

        $response = $this->deleteJson("/api/reviews/{$review->id}");

        $response->assertStatus(200)
                 ->assertJson(['success' => true]);
        
        $this->assertDatabaseMissing('product_reviews', [
            'id' => $review->id,
        ]);
    }

    public function test_admin_can_approve_review()
    {
        $admin = User::factory()->create();
        $admin->assignRole('admin');
        
        $category = Category::factory()->create();
        $product = Product::factory()->create(['category_id' => $category->id]);
        
        $review = ProductReview::factory()->create([
            'product_id' => $product->id,
            'is_approved' => false,
        ]);
        
        $this->actingAs($admin);

        $response = $this->patchJson("/admin/reviews/{$review->id}/approve", [
            'is_approved' => true,
        ]);

        $response->assertStatus(200)
                 ->assertJson(['success' => true]);
        
        $this->assertDatabaseHas('product_reviews', [
            'id' => $review->id,
            'is_approved' => true,
        ]);
    }

    public function test_rating_recalculated_after_review_approved()
    {
        $admin = User::factory()->create();
        $admin->assignRole('admin');
        
        $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,
            'rating' => 5,
            'is_approved' => true,
        ]);
        
        $review = ProductReview::factory()->create([
            'product_id' => $product->id,
            'user_id' => $user->id,
            'rating' => 3,
            'is_approved' => false,
        ]);
        
        $this->actingAs($admin);

        $this->patchJson("/admin/reviews/{$review->id}/approve", [
            'is_approved' => true,
        ]);

        $product->refresh();
        
        // Average of 5 and 3 = 4.0
        $this->assertEquals(4.0, $product->avg_rating);
        $this->assertEquals(2, $product->rating_count);
    }
}

