<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Coupon;
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\Tenant;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class DefaultShopSeeder extends Seeder
{
    public function run(): void
    {
        $tenant = Tenant::query()->first();
        if (! $tenant) {
            $this->command?->warn('No tenant found — skipping DefaultShopSeeder');
            return;
        }
        $tenantId = $tenant->getKey();
        app()->instance('currentTenant', $tenant);

        $cats = [
            ['name' => '의류', 'slug' => 'apparel'],
            ['name' => '식품', 'slug' => 'food'],
            ['name' => '디지털', 'slug' => 'digital'],
            ['name' => '도서', 'slug' => 'books'],
            ['name' => '가구', 'slug' => 'furniture'],
            ['name' => '기타', 'slug' => 'misc'],
        ];
        $catModels = [];
        $sort = 0;
        foreach ($cats as $c) {
            $catModels[$c['slug']] = ProductCategory::updateOrCreate(
                ['slug' => $c['slug']],
                ['name' => $c['name'], 'sort_order' => $sort++],
            );
        }

        $samples = [
            'apparel' => ['Premium Tee', 'Hoodie', 'Denim Jacket', 'Sneakers', 'Cap', 'Backpack', 'Socks Pack', 'Scarf'],
            'food' => ['Coffee Beans 1kg', 'Organic Honey', 'Granola', 'Olive Oil', 'Tea Set', 'Chocolate Box', 'Pasta', 'Spice Pack'],
            'digital' => ['eBook Reader', 'USB-C Hub', 'Wireless Mouse', 'Bluetooth Speaker', 'Webcam HD', 'Earbuds', 'Power Bank', 'Smart Plug'],
            'books' => ['Design Patterns', 'Clean Code', 'The Pragmatic Programmer', 'Atomic Habits', 'Refactoring', 'Domain-Driven Design', 'Sapiens', 'Hooked'],
            'furniture' => ['Wooden Chair', 'Standing Desk', 'Bookshelf', 'Floor Lamp', 'Office Chair', 'Side Table'],
            'misc' => ['Mug Set', 'Notebook', 'Pen Pack', 'Plant Pot', 'Aroma Diffuser', 'Wall Clock'],
        ];

        $count = 0;
        foreach ($samples as $catSlug => $names) {
            foreach ($names as $i => $name) {
                $price = mt_rand(8, 200) * 1000;
                $sale = mt_rand(0, 2) === 0 ? (int) ($price * 0.8) : null;
                Product::updateOrCreate(
                    ['tenant_id' => $tenantId, 'slug' => Str::slug($name)],
                    [
                        'name' => $name,
                        'description' => $name . ' — quality product from W·Dot Shop.',
                        'category_id' => $catModels[$catSlug]->id,
                        'brand' => 'W·Dot',
                        'sku' => 'WDOT-' . strtoupper(Str::random(8)),
                        'price' => $price,
                        'sale_price' => $sale,
                        'currency' => 'KRW',
                        'stock_qty' => mt_rand(5, 200),
                        'status' => 'active',
                        'thumb_url' => 'https://picsum.photos/seed/' . urlencode($name) . '/600/400',
                        'meta' => ['featured' => $i === 0],
                    ],
                );
                $count++;
            }
        }

        $coupons = [
            ['code' => 'WELCOME10', 'type' => 'percent', 'value' => 10, 'min_amount' => 0],
            ['code' => 'FREESHIP', 'type' => 'free_ship', 'value' => 0, 'min_amount' => 30000],
            ['code' => 'SUMMER15', 'type' => 'percent', 'value' => 15, 'min_amount' => 50000],
            ['code' => 'FLAT5000', 'type' => 'fixed', 'value' => 5000, 'min_amount' => 20000],
            ['code' => 'VIP20', 'type' => 'percent', 'value' => 20, 'min_amount' => 100000],
        ];
        foreach ($coupons as $c) {
            Coupon::updateOrCreate(
                ['tenant_id' => $tenantId, 'code' => $c['code']],
                array_merge($c, [
                    'max_uses' => 1000,
                    'used_count' => 0,
                    'valid_from' => now()->subDay(),
                    'valid_until' => now()->addYear(),
                    'status' => 'active',
                ]),
            );
        }

        $this->command?->info('Shop seeded: ' . $count . ' products / 5 coupons / ' . count($cats) . ' categories.');
    }
}
