<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Models\Market\Product;
use App\Models\Market\Store;
use App\Models\User;
use Hypervel\Console\Command;
use Hypervel\Support\Facades\Auth;

class SeedDemoProducts extends Command
{
    protected ?string $signature = 'demo:seed-products {store_hash : The hashkey of the target store}';

    protected string $description = 'Seed demo agricultural products';

    public function handle()
    {
        $storeHash = $this->argument('store_hash');

        $store = Store::where('hashkey', $storeHash)->first();
        if (! $store) {
            $this->error('Store not found.');
            return;
        }

        $ultimateUser = User::where('acct_type', 'ULTIMATE')->first();
        if (! $ultimateUser) {
            $this->error('No ULTIMATE user found.');
            return;
        }

        Auth::loginUsingId($ultimateUser->id);

        $products = [
            ['name' => 'Organic White Rice (25kg sack)', 'category' => 'Grains & Cereals', 'subcategory' => 'Rice', 'price' => 1200, 'unit' => 'sack', 'available' => 100],
            ['name' => 'Organic Brown Rice (5kg)', 'category' => 'Grains & Cereals', 'subcategory' => 'Rice', 'price' => 280, 'unit' => 'kg', 'available' => 200],
            ['name' => 'White Corn Grits', 'category' => 'Grains & Cereals', 'subcategory' => 'Corn', 'price' => 85, 'unit' => 'kg', 'available' => 300],
            ['name' => 'Yellow Corn (fresh ears)', 'category' => 'Grains & Cereals', 'subcategory' => 'Corn', 'price' => 35, 'unit' => 'piece', 'available' => 500],
            ['name' => 'Fresh Kangkong (Water Spinach)', 'category' => 'Fresh Produce', 'subcategory' => 'Leafy Vegetables', 'price' => 25, 'unit' => 'bundle', 'available' => 150],
            ['name' => 'Pechay (Bok Choy)', 'category' => 'Fresh Produce', 'subcategory' => 'Leafy Vegetables', 'price' => 30, 'unit' => 'bundle', 'available' => 150],
            ['name' => 'Ampalaya (Bitter Gourd)', 'category' => 'Fresh Produce', 'subcategory' => 'Vegetables', 'price' => 60, 'unit' => 'kg', 'available' => 100],
            ['name' => 'Sitaw (String Beans)', 'category' => 'Fresh Produce', 'subcategory' => 'Vegetables', 'price' => 55, 'unit' => 'bundle', 'available' => 120],
            ['name' => 'Kamote (Sweet Potato)', 'category' => 'Fresh Produce', 'subcategory' => 'Root Crops', 'price' => 40, 'unit' => 'kg', 'available' => 200],
            ['name' => 'Gabi (Taro Root)', 'category' => 'Fresh Produce', 'subcategory' => 'Root Crops', 'price' => 50, 'unit' => 'kg', 'available' => 150],
            ['name' => 'Sayote (Chayote)', 'category' => 'Fresh Produce', 'subcategory' => 'Vegetables', 'price' => 45, 'unit' => 'kg', 'available' => 180],
            ['name' => 'Labanos (Radish)', 'category' => 'Fresh Produce', 'subcategory' => 'Root Crops', 'price' => 30, 'unit' => 'bundle', 'available' => 100],
            ['name' => 'Fresh Tomatoes', 'category' => 'Fresh Produce', 'subcategory' => 'Vegetables', 'price' => 70, 'unit' => 'kg', 'available' => 200],
            ['name' => 'Carabao Mango (green)', 'category' => 'Fresh Produce', 'subcategory' => 'Fruits', 'price' => 90, 'unit' => 'kg', 'available' => 100],
            ['name' => 'Banana (Latundan)', 'category' => 'Fresh Produce', 'subcategory' => 'Fruits', 'price' => 65, 'unit' => 'hand', 'available' => 80],
            ['name' => 'Pineapple (Bukidnon)', 'category' => 'Fresh Produce', 'subcategory' => 'Fruits', 'price' => 80, 'unit' => 'piece', 'available' => 60],
            ['name' => 'Native Chicken (live)', 'category' => 'Livestock & Poultry', 'subcategory' => 'Poultry', 'price' => 350, 'unit' => 'piece', 'available' => 30],
            ['name' => 'Free-range Eggs', 'category' => 'Livestock & Poultry', 'subcategory' => 'Eggs', 'price' => 12, 'unit' => 'piece', 'available' => 500],
            ['name' => 'Fresh Tilapia', 'category' => 'Seafood', 'subcategory' => 'Freshwater Fish', 'price' => 130, 'unit' => 'kg', 'available' => 50],
            ['name' => 'Bangus (Milkfish)', 'category' => 'Seafood', 'subcategory' => 'Saltwater Fish', 'price' => 180, 'unit' => 'kg', 'available' => 50],
            ['name' => 'Carabao Milk (1L)', 'category' => 'Processed Goods', 'subcategory' => 'Dairy', 'price' => 120, 'unit' => 'liter', 'available' => 60],
            ['name' => 'Coconut Oil (Virgin, 1L)', 'category' => 'Processed Goods', 'subcategory' => 'Oils', 'price' => 250, 'unit' => 'bottle', 'available' => 40],
            ['name' => 'Organic Peanuts (roasted)', 'category' => 'Processed Goods', 'subcategory' => 'Nuts', 'price' => 95, 'unit' => '250g pack', 'available' => 80],
            ['name' => 'Muscovado Sugar', 'category' => 'Processed Goods', 'subcategory' => 'Sweeteners', 'price' => 110, 'unit' => '250g pack', 'available' => 60],
            ['name' => 'Fresh Turmeric (Luyang Dilaw)', 'category' => 'Fresh Produce', 'subcategory' => 'Herbs & Spices', 'price' => 60, 'unit' => '100g pack', 'available' => 100],
        ];

        $created = [];

        foreach ($products as $item) {
            $product = new Product();
            $product->name = $item['name'];
            $product->category = $item['category'];
            $product->subcategory = $item['subcategory'];
            $product->price = $item['price'];
            $product->unitname = $item['unit'];
            $product->description = $item['name'];
            $product->is_active = true;
            $product->save();

            $store->products()->attach($product->id, [
                'price'     => $item['price'],
                'available' => $item['available'],
                'is_active' => true,
            ]);

            $created[] = $product;
        }

        $count = count($created);
        $this->info("Created {$count} products and attached to store {$store->name}");
    }
}
