<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Product;
use App\Models\Category;
use App\Models\Cart;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CartFunctionalityTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_add_product_to_cart()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create([
            'category_id' => $category->id,
            'current_stock' => 10,
            'price' => 100.00
        ]);

        $response = $this->actingAs($user)->post('/cart', [
            'product_id' => $product->id,
            'qty' => 2
        ]);

        $response->assertRedirect();
        $this->assertDatabaseHas('cart_items', [
            'product_id' => $product->id,
            'qty' => 2
        ]);
    }

    public function test_cannot_add_more_than_available_stock()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        $product = Product::factory()->create([
            'category_id' => $category->id,
            'current_stock' => 5,
            'price' => 100.00
        ]);

        $response = $this->actingAs($user)->post('/cart', [
            'product_id' => $product->id,
            'qty' => 10 // More than available stock
        ]);

        $response->assertRedirect();
        $response->assertSessionHas('error');
        $this->assertDatabaseMissing('cart_items', [
            'product_id' => $product->id
        ]);
    }

    public function test_cart_calculates_totals_correctly()
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();
        
        $product1 = Product::factory()->create([
            'category_id' => $category->id,
            'current_stock' => 10,
            'price' => 100.00
        ]);
        
        $product2 = Product::factory()->create([
            'category_id' => $category->id,
            'current_stock' => 10,
            'price' => 50.00
        ]);

        // Add products to cart
        $this->actingAs($user)->post('/cart', [
            'product_id' => $product1->id,
            'qty' => 2
        ]);

        $this->actingAs($user)->post('/cart', [
            'product_id' => $product2->id,
            'qty' => 1
        ]);

        $response = $this->actingAs($user)->get('/cart');
        $response->assertStatus(200);

        // Check if cart total is calculated correctly (2 * 100 + 1 * 50 = 250)
        $cart = Cart::where('user_id', $user->id)->first();
        $this->assertNotNull($cart);
    }

    public function test_guest_cart_uses_session()
    {
        $category = Category::factory()->create();
        $product = Product::factory()->create([
            'category_id' => $category->id,
            'current_stock' => 10,
            'price' => 100.00
        ]);

        $response = $this->post('/cart', [
            'product_id' => $product->id,
            'qty' => 1
        ]);

        $response->assertRedirect();
        
        // Should create cart with cookie_token instead of user_id
        $this->assertDatabaseHas('carts', [
            'user_id' => null
        ]);
    }
}