<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Product;
use App\Models\ProductVariant;
use App\Models\StockAdjustment;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Livewire\Livewire;

class BulkEditStockTest extends TestCase
{
    use RefreshDatabase;

    private User $admin;
    private Product $simpleProduct;
    private Product $parentProduct;
    private ProductVariant $variant;

    protected function setUp(): void
    {
        parent::setUp();

        // Seed admin user
        $this->admin = User::create([
            'name' => 'Admin User',
            'email' => 'admin@sawdabazar.com',
            'password' => bcrypt('password123'),
            'role' => 'admin',
        ]);

        $category = \App\Models\Category::create([
            'name' => 'Electronics',
            'slug' => 'electronics',
            'is_visible' => true,
        ]);

        // Simple product
        $this->simpleProduct = Product::create([
            'name' => 'Smart Watch',
            'slug' => 'smart-watch',
            'sku' => 'SW-001',
            'price' => 2000.00,
            'quantity' => 50,
            'security_stock' => 10,
            'published_at' => now(),
            'category_id' => $category->id,
        ]);

        // Parent product with variants
        $this->parentProduct = Product::create([
            'name' => 'Polo T-Shirt',
            'slug' => 'polo-t-shirt',
            'sku' => 'PTS-002',
            'price' => 1000.00,
            'quantity' => 30,
            'security_stock' => 5,
            'published_at' => now(),
            'category_id' => $category->id,
        ]);

        $this->variant = ProductVariant::create([
            'product_id' => $this->parentProduct->id,
            'sku' => 'PTS-002-M-RED',
            'price' => 1000.00,
            'quantity' => 30,
            'security_stock' => 5,
            'options' => ['Size' => 'M', 'Color' => 'Red'],
        ]);
    }

    /**
     * Test bulk edit stock page renders for admins.
     */
    public function test_bulk_edit_page_renders_correctly(): void
    {
        $this->actingAs($this->admin);

        $response = $this->get('/admin/bulk-edit-stock');
        $response->assertStatus(200);

        Livewire::test(\App\Filament\Pages\BulkEditStock::class)
            ->assertSee('Bulk Edit Stock')
            ->assertSee('SW-001')
            ->assertSee('PTS-002-M-RED');
    }

    /**
     * Test bulk edit stock page search filters products.
     */
    public function test_bulk_edit_page_filters_by_search(): void
    {
        $this->actingAs($this->admin);

        Livewire::test(\App\Filament\Pages\BulkEditStock::class)
            ->set('search', 'Smart Watch')
            ->assertSee('SW-001')
            ->assertDontSee('PTS-002-M-RED');

        Livewire::test(\App\Filament\Pages\BulkEditStock::class)
            ->set('search', 'PTS-002')
            ->assertSee('PTS-002-M-RED')
            ->assertDontSee('SW-001');
    }

    /**
     * Test bulk edit page saves stock adjustments and safety stocks correctly.
     */
    public function test_bulk_edit_page_saves_stock_adjustments(): void
    {
        $this->actingAs($this->admin);

        Livewire::test(\App\Filament\Pages\BulkEditStock::class)
            // Edit simple product: qty 50 -> 40, security stock 10 -> 15
            ->set('quantities.product.' . $this->simpleProduct->id, 40)
            ->set('securityStocks.product.' . $this->simpleProduct->id, 15)
            // Edit variant: qty 30 -> 35, security stock 5 -> 8
            ->set('quantities.variant.' . $this->variant->id, 35)
            ->set('securityStocks.variant.' . $this->variant->id, 8)
            ->call('save')
            ->assertHasNoErrors()
            ->assertDispatched('saved');

        // Check simple product
        $this->assertEquals(40, $this->simpleProduct->fresh()->quantity);
        $this->assertEquals(15, $this->simpleProduct->fresh()->security_stock);

        // Check variant (since variant stock went up, parent product stock should go from 30 -> 35 too)
        $this->assertEquals(35, $this->variant->fresh()->quantity);
        $this->assertEquals(8, $this->variant->fresh()->security_stock);
        $this->assertEquals(35, $this->parentProduct->fresh()->quantity);

        // Check stock adjustments logs exist
        $this->assertDatabaseHas('stock_adjustments', [
            'product_id' => $this->simpleProduct->id,
            'product_variant_id' => null,
            'change_amount' => -10,
            'old_quantity' => 50,
            'new_quantity' => 40,
            'event' => 'manual',
        ]);

        $this->assertDatabaseHas('stock_adjustments', [
            'product_id' => $this->parentProduct->id,
            'product_variant_id' => $this->variant->id,
            'change_amount' => 5,
            'old_quantity' => 30,
            'new_quantity' => 35,
            'event' => 'manual',
        ]);
    }
}
