<?php

namespace Tests\Feature;

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

class ProductWeightFormTest extends TestCase
{
    use RefreshDatabase;

    private User $admin;
    private $category;

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

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

        $this->category = \App\Models\Category::create([
            'name' => 'Test Category',
            'slug' => 'test-category',
            'is_visible' => true,
        ]);
    }

    /**
     * Test creating a product with weight in kilograms and ensuring it saves correctly in grams.
     */
    public function test_can_create_product_with_kilograms_weight(): void
    {
        Livewire::actingAs($this->admin);
        \Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('admin'));

        Livewire::test(\App\Filament\Resources\Products\Pages\CreateProduct::class)
            ->fillForm([
                'name' => 'Heavy Box',
                'price' => 100.00,
                'compare_at_price' => 120.00,
                'cost_price' => 80.00,
                'quantity' => 5,
                'published_at' => now(),
                'category_id' => $this->category->id,
                'is_weight_based' => true, // Enabled weight-based
                'weight_unit' => 'kg', // kilograms selected first
                'weight_grams' => 1.5, // then input the weight value
            ])
            ->call('create')
            ->assertHasNoFormErrors();

        $product = Product::where('slug', 'heavy-box')->first();
        $this->assertNotNull($product);
        // 1.5 kg should be stored as 1500 grams
        $this->assertEquals(1500, $product->weight_grams);
        $this->assertTrue($product->is_weight_based);
    }

    /**
     * Test editing a product with weight >= 1000g hydrates the form as kilograms.
     */
    public function test_edit_product_with_large_weight_hydrates_as_kilograms(): void
    {
        $product = Product::create([
            'name' => 'Big Weight Product',
            'slug' => 'big-weight-product',
            'sku' => 'BWP-001',
            'price' => 100.00,
            'compare_at_price' => 120.00,
            'cost_price' => 80.00,
            'quantity' => 5,
            'published_at' => now(),
            'category_id' => $this->category->id,
            'is_weight_based' => true,
            'weight_grams' => 1000, // 1000 grams stored
        ]);

        Livewire::actingAs($this->admin);
        \Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('admin'));

        Livewire::test(\App\Filament\Resources\Products\Pages\EditProduct::class, [
            'record' => $product->id,
        ])
            ->assertFormSet([
                'is_weight_based' => true,
                'weight_grams' => 1, // should display as 1
                'weight_unit' => 'kg', // unit select should show kg
            ]);
    }

    /**
     * Test editing a product with weight < 1000g hydrates the form as grams.
     */
    public function test_edit_product_with_small_weight_hydrates_as_grams(): void
    {
        $product = Product::create([
            'name' => 'Small Weight Product',
            'slug' => 'small-weight-product',
            'sku' => 'SWP-001',
            'price' => 100.00,
            'compare_at_price' => 120.00,
            'cost_price' => 80.00,
            'quantity' => 5,
            'published_at' => now(),
            'category_id' => $this->category->id,
            'is_weight_based' => true,
            'weight_grams' => 350, // 350 grams stored
        ]);

        Livewire::actingAs($this->admin);
        \Filament\Facades\Filament::setCurrentPanel(\Filament\Facades\Filament::getPanel('admin'));

        Livewire::test(\App\Filament\Resources\Products\Pages\EditProduct::class, [
            'record' => $product->id,
        ])
            ->assertFormSet([
                'is_weight_based' => true,
                'weight_grams' => 350, // should display as 350
                'weight_unit' => 'g', // unit select should show g
            ]);
    }
}
