<?php

namespace Tests\Feature;

use App\Models\Product;
use App\Models\Sale;
use App\Models\User;
use App\Models\Warehouse;
use App\Models\WarehouseStock;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SaleStockValidationTest extends TestCase
{
    use RefreshDatabase;

    public function test_store_rejects_sale_when_stock_is_zero(): void
    {
        $user = User::factory()->create();
        $warehouse = Warehouse::create(['code' => 'WH1', 'name' => 'Gudang Utama']);
        $product = Product::create(['sku' => 'SKU-ZERO', 'name' => 'Produk Habis', 'sell_price' => 10000]);

        WarehouseStock::create([
            'warehouse_id'  => $warehouse->id,
            'product_id'    => $product->id,
            'current_stock' => 0,
            'min_stock'     => 5,
        ]);

        $response = $this->actingAs($user, 'sanctum')->postJson('/api/sales', [
            'order_no'     => 'ORD-ZERO',
            'channel'      => 'tokopedia',
            'warehouse_id' => $warehouse->id,
            'items' => [
                ['product_id' => $product->id, 'qty' => 1, 'price' => 10000],
            ],
        ]);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors('items');
        $this->assertNull(Sale::where('order_no', 'ORD-ZERO')->first());
    }

    public function test_store_rejects_sale_when_qty_exceeds_available_stock(): void
    {
        $user = User::factory()->create();
        $warehouse = Warehouse::create(['code' => 'WH1', 'name' => 'Gudang Utama']);
        $product = Product::create(['sku' => 'SKU-LOW', 'name' => 'Produk Sedikit', 'sell_price' => 10000]);

        WarehouseStock::create([
            'warehouse_id'  => $warehouse->id,
            'product_id'    => $product->id,
            'current_stock' => 3,
            'min_stock'     => 5,
        ]);

        $response = $this->actingAs($user, 'sanctum')->postJson('/api/sales', [
            'order_no'     => 'ORD-LOW',
            'channel'      => 'tokopedia',
            'warehouse_id' => $warehouse->id,
            'items' => [
                ['product_id' => $product->id, 'qty' => 5, 'price' => 10000],
            ],
        ]);

        $response->assertStatus(422);
        $response->assertJsonValidationErrors('items');
        $this->assertNull(Sale::where('order_no', 'ORD-LOW')->first());
    }

    public function test_store_succeeds_when_stock_is_sufficient(): void
    {
        $user = User::factory()->create();
        $warehouse = Warehouse::create(['code' => 'WH1', 'name' => 'Gudang Utama']);
        $product = Product::create(['sku' => 'SKU-OK', 'name' => 'Produk Ada', 'sell_price' => 10000]);

        WarehouseStock::create([
            'warehouse_id'  => $warehouse->id,
            'product_id'    => $product->id,
            'current_stock' => 10,
            'min_stock'     => 5,
        ]);

        $response = $this->actingAs($user, 'sanctum')->postJson('/api/sales', [
            'order_no'     => 'ORD-OK',
            'channel'      => 'tokopedia',
            'warehouse_id' => $warehouse->id,
            'items' => [
                ['product_id' => $product->id, 'qty' => 4, 'price' => 10000],
            ],
        ]);

        $response->assertStatus(201);
        $this->assertNotNull(Sale::where('order_no', 'ORD-OK')->first());
    }
}
