<?php

namespace Database\Seeders;

use App\Models\Category;
use App\Models\Product;
use App\Models\Unit;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Demo products for Reports → Inventory → Low Stock and Out of Stock.
     */
    public function run(): void
    {
        $categoryId = Category::query()->value('id');
        $unitId = Unit::query()->value('id');
        $userId = User::query()->value('id');

        // Low stock: 0 < stock <= alert_quantity
        $demos = [
            ['code' => 'DEMO-LOW-001', 'name' => 'Demo Low Stock — Thermal Labels (roll)', 'stock' => 2, 'alert' => 15, 'purchase' => 12.5],
            ['code' => 'DEMO-LOW-002', 'name' => 'Demo Low Stock — USB-C Hub 4-port', 'stock' => 4, 'alert' => 20, 'purchase' => 28.0],
            ['code' => 'DEMO-LOW-003', 'name' => 'Demo Low Stock — AA Batteries (pack)', 'stock' => 6, 'alert' => 24, 'purchase' => 8.99],
            ['code' => 'DEMO-LOW-004', 'name' => 'Demo Low Stock — Network Cable Cat6 (3m)', 'stock' => 3, 'alert' => 10, 'purchase' => 5.5],
            ['code' => 'DEMO-LOW-005', 'name' => 'Demo Low Stock — Office Sticky Notes', 'stock' => 1, 'alert' => 8, 'purchase' => 3.25],
        ];

        foreach ($demos as $row) {
            Product::updateOrCreate(
                ['code' => $row['code']],
                [
                    'name' => $row['name'],
                    'type' => 'product',
                    'description' => 'Seeded demo product for low-stock report.',
                    'category_id' => $categoryId,
                    'unit_id' => $unitId,
                    'selling_price' => $row['purchase'] * 1.2,
                    'purchase_price' => $row['purchase'],
                    'stock' => $row['stock'],
                    'alert_quantity' => $row['alert'],
                    'status' => true,
                    'created_by' => $userId,
                ]
            );
        }

        // Out of stock: stock === 0
        $oos = [
            ['code' => 'DEMO-OOS-001', 'name' => 'Demo Out of Stock — Ergonomic Keyboard', 'alert' => 5, 'purchase' => 79.99],
            ['code' => 'DEMO-OOS-002', 'name' => 'Demo Out of Stock — Webcam HD Pro', 'alert' => 8, 'purchase' => 65.0],
            ['code' => 'DEMO-OOS-003', 'name' => 'Demo Out of Stock — Desk Lamp LED', 'alert' => 12, 'purchase' => 34.5],
            ['code' => 'DEMO-OOS-004', 'name' => 'Demo Out of Stock — HDMI Switch 5×1', 'alert' => 6, 'purchase' => 42.0],
            ['code' => 'DEMO-OOS-005', 'name' => 'Demo Out of Stock — Bluetooth Speaker', 'alert' => 10, 'purchase' => 55.0],
        ];

        foreach ($oos as $row) {
            Product::updateOrCreate(
                ['code' => $row['code']],
                [
                    'name' => $row['name'],
                    'type' => 'product',
                    'description' => 'Seeded demo product for out-of-stock report.',
                    'category_id' => $categoryId,
                    'unit_id' => $unitId,
                    'selling_price' => $row['purchase'] * 1.25,
                    'purchase_price' => $row['purchase'],
                    'stock' => 0,
                    'alert_quantity' => $row['alert'],
                    'status' => true,
                    'created_by' => $userId,
                ]
            );
        }

        $this->command->info('Seeded 5 demo low-stock (DEMO-LOW-*) and 5 demo out-of-stock (DEMO-OOS-*) products.');
    }
}
