<?php

namespace Database\Seeders;

use App\Models\Admin;
use App\Models\ChannelConfig;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\PaymentConfig;
use App\Models\Plan;
use App\Models\PlatformOrder;
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\SiteSubscription;
use App\Models\SystemConfig;
use App\Models\Merchant;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class InitialDemoSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedPlatformConfigs();

        $merchant = Merchant::query()->firstOrCreate(
            ['slug' => 'demo-shop'],
            [
                'name' => '演示店铺',
                'domain' => null,
                'contact_name' => '林哥',
                'contact_phone' => '13800000000',
                'contact_email' => 'demo@example.com',
                'plan' => 'pro',
                'status' => 'active',
                'activated_at' => now(),
                'settings' => ['currency' => 'CNY'],
            ]
        );

        $platformAdmin = Admin::query()->updateOrCreate(
            ['email' => 'platform.admin@demo.local'],
            [
                'merchant_id' => null,
                'name' => '平台管理员',
                'phone' => '13900000000',
                'password' => Hash::make('Platform@123456'),
                'role' => 'platform_owner',
                'status' => 'active',
            ]
        );

        $merchantAdmin = Admin::query()->updateOrCreate(
            ['email' => 'merchant.admin@demo.local'],
            [
                'merchant_id' => $merchant->id,
                'name' => '商家管理员',
                'phone' => '13900000001',
                'password' => Hash::make('Merchant@123456'),
                'role' => 'merchant_owner',
                'status' => 'active',
            ]
        );

        Admin::query()->where('email', 'admin@demo.local')->delete();

        $user = User::query()->firstOrCreate(
            ['email' => 'user@demo.local'],
            [
                'merchant_id' => $merchant->id,
                'name' => '演示用户',
                'phone' => '13700000000',
                'password' => Hash::make('User@123456'),
                'status' => 'active',
                'register_source' => 'pc',
                'last_login_source' => 'pc',
            ]
        );

        $defaultCategory = ProductCategory::query()->firstOrCreate(
            ['merchant_id' => $merchant->id, 'slug' => 'default'],
            [
                'name' => '默认分类',
                'status' => 'active',
                'sort' => 10,
                'description' => '系统初始化生成的默认商品分类',
            ]
        );

        $featuredCategory = ProductCategory::query()->firstOrCreate(
            ['merchant_id' => $merchant->id, 'slug' => 'featured'],
            [
                'name' => '精选商品',
                'status' => 'active',
                'sort' => 20,
                'description' => '用于首页和推荐位的精选分类',
            ]
        );

        $product = Product::query()->firstOrCreate(
            ['sku' => 'SKU-DEMO-001'],
            [
                'merchant_id' => $merchant->id,
                'category_id' => $defaultCategory->id,
                'title' => '演示商品',
                'slug' => 'demo-product',
                'summary' => '这是 SaaS 电商项目的演示商品。',
                'content' => '后续可扩展商品详情、规格、库存与多图。',
                'price' => 199.00,
                'original_price' => 299.00,
                'stock' => 100,
                'status' => 'published',
                'images' => ['https://via.placeholder.com/600x600.png?text=Demo+Product'],
            ]
        );

        $plans = [
            [
                'code' => 'starter_monthly',
                'name' => '基础版（月付）',
                'billing_cycle' => 'monthly',
                'price' => 99,
                'list_price' => 129,
                'status' => 'active',
                'sort' => 10,
                'description' => '适合刚开站的试运营阶段，可用于 Demo 场景。',
                'published_at' => now()->subDays(5),
            ],
            [
                'code' => 'pro_yearly',
                'name' => '专业版（年付）',
                'billing_cycle' => 'yearly',
                'price' => 1999,
                'list_price' => 2599,
                'status' => 'active',
                'sort' => 20,
                'description' => '面向成长型站点，后续搭配授权项配置。',
                'published_at' => now()->subDays(3),
            ],
            // 用于仪表盘“套餐订单占比（Top5）”演示：额外补一些不同档位的套餐
            [
                'code' => 'growth_monthly',
                'name' => '成长版（月付）',
                'billing_cycle' => 'monthly',
                'price' => 199,
                'list_price' => 239,
                'status' => 'active',
                'sort' => 30,
                'description' => '用于验证套餐占比/收入分布。',
                'published_at' => now()->subDays(2),
            ],
            [
                'code' => 'team_monthly',
                'name' => '团队版（月付）',
                'billing_cycle' => 'monthly',
                'price' => 399,
                'list_price' => 499,
                'status' => 'active',
                'sort' => 40,
                'description' => '用于验证 Top5 占比卡多行展示。',
                'published_at' => now()->subDays(2),
            ],
            [
                'code' => 'enterprise_yearly',
                'name' => '企业版（年付）',
                'billing_cycle' => 'yearly',
                'price' => 9999,
                'list_price' => 12999,
                'status' => 'active',
                'sort' => 50,
                'description' => '用于验证高客单价档位展示。',
                'published_at' => now()->subDays(1),
            ],
        ];

        foreach ($plans as $planData) {
            Plan::query()->updateOrCreate(
                ['code' => $planData['code']],
                $planData
            );
        }

        // 用于仪表盘：补齐「套餐订单占比（Top5）」与「站点收入排行 Top5」所需的多维演示数据
        // 原则：
        // - 站点名称不含“演示”，避免命中某些测试用例的 keyword=演示
        // - plan 字段不设为 pro，避免命中某些测试用例的 plan=pro
        // - 平台订单补齐回执证据 & 写入 subscription_activation.subscription_id，避免仪表盘最近订单出现“无回执/可同步”等治理提示干扰
        $planIds = [
            'starter_monthly' => (int) Plan::query()->where('code', 'starter_monthly')->value('id'),
            'pro_yearly' => (int) Plan::query()->where('code', 'pro_yearly')->value('id'),
            'growth_monthly' => (int) Plan::query()->where('code', 'growth_monthly')->value('id'),
            'team_monthly' => (int) Plan::query()->where('code', 'team_monthly')->value('id'),
            'enterprise_yearly' => (int) Plan::query()->where('code', 'enterprise_yearly')->value('id'),
        ];

        $extraMerchants = [
            ['slug' => 'demo-site-b', 'name' => '样例站点B', 'plan' => 'starter'],
            ['slug' => 'demo-site-c', 'name' => '样例站点C', 'plan' => 'starter'],
            ['slug' => 'demo-site-d', 'name' => '样例站点D', 'plan' => 'starter'],
            ['slug' => 'demo-site-e', 'name' => '样例站点E', 'plan' => 'starter'],
        ];

        foreach ($extraMerchants as $m) {
            Merchant::query()->firstOrCreate(
                ['slug' => $m['slug']],
                [
                    'name' => $m['name'],
                    'domain' => null,
                    'contact_name' => '样例联系人',
                    'contact_phone' => '13800000000',
                    'contact_email' => 'demo@example.com',
                    'plan' => (string) ($m['plan'] ?? 'starter'),
                    'status' => 'active',
                    'activated_at' => now(),
                    'settings' => ['currency' => 'CNY'],
                ]
            );
        }

        // 套餐占比（Top5）演示：近 7 天各套餐造不同数量的订单，形成 Top5 梯度
        // 说明：占比卡按“订单数”统计，不以 paid_sum 作为分母。
        $shareSeed = [
            // starter：8 单
            ['plan_code' => 'starter_monthly', 'cnt' => 8, 'amount' => 99],
            // pro：6 单
            ['plan_code' => 'pro_yearly', 'cnt' => 6, 'amount' => 1999],
            // growth：4 单
            ['plan_code' => 'growth_monthly', 'cnt' => 4, 'amount' => 199],
            // team：3 单
            ['plan_code' => 'team_monthly', 'cnt' => 3, 'amount' => 399],
            // enterprise：2 单
            ['plan_code' => 'enterprise_yearly', 'cnt' => 2, 'amount' => 9999],
        ];

        $merchantSlugsForShare = ['demo-shop', 'demo-site-b', 'demo-site-c', 'demo-site-d', 'demo-site-e'];
        $merchantsBySlug = Merchant::query()->whereIn('slug', $merchantSlugsForShare)->get()->keyBy('slug');

        $seq = 1;
        foreach ($shareSeed as $seed) {
            $planCode = (string) ($seed['plan_code'] ?? '');
            $planId = (int) ($planIds[$planCode] ?? 0);
            if ($planId <= 0) {
                continue;
            }

            $cnt = (int) ($seed['cnt'] ?? 0);
            $cnt = max(0, min(50, $cnt));
            $amount = (float) ($seed['amount'] ?? 0);

            for ($i = 0; $i < $cnt; $i++) {
                $slug = $merchantSlugsForShare[($seq - 1) % count($merchantSlugsForShare)];
                $mch = $merchantsBySlug->get($slug);
                if (! $mch) {
                    $seq++;
                    continue;
                }

                // 分散到近 7 天，避免趋势图/排行过于集中到某一天
                $daysAgo = ($seq - 1) % 7;
                $createdAt = now()->subDays($daysAgo)->setTime(12, 0, 0);

                $orderNo = 'PO_DEMO_SHARE_' . strtoupper(substr($planCode, 0, 3)) . '_' . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);

                PlatformOrder::query()->updateOrCreate(
                    ['order_no' => $orderNo],
                    [
                        'merchant_id' => $mch->id,
                        'plan_id' => $planId,
                        'site_subscription_id' => null,
                        'created_by_admin_id' => $platformAdmin->id,
                        'order_type' => 'new_purchase',
                        'status' => 'activated',
                        'payment_status' => 'paid',
                        'payment_channel' => 'manual',
                        'plan_name' => (string) Plan::query()->where('id', $planId)->value('name'),
                        'billing_cycle' => (string) Plan::query()->where('id', $planId)->value('billing_cycle'),
                        'period_months' => 1,
                        'quantity' => 1,
                        'list_amount' => $amount,
                        'discount_amount' => 0,
                        'payable_amount' => $amount,
                        'paid_amount' => $amount,
                        'placed_at' => $createdAt,
                        'paid_at' => $createdAt,
                        'activated_at' => $createdAt,
                        'meta' => [
                            'payment_summary' => [
                                'total_amount' => $amount,
                            ],
                            'payment_receipts' => [
                                [
                                    'amount' => $amount,
                                    'channel' => 'manual',
                                    'paid_at' => $createdAt->toDateTimeString(),
                                ],
                            ],
                            'subscription_activation' => [
                                'subscription_id' => 20000 + $seq,
                            ],
                            'note' => '初始化占比演示订单',
                        ],
                        'remark' => '初始化占比演示订单',
                        'created_at' => $createdAt,
                        'updated_at' => $createdAt,
                    ]
                );

                $seq++;
            }
        }

        $subscription = SiteSubscription::query()->firstOrCreate(
            ['subscription_no' => 'SUB202603100001'],
            [
                'merchant_id' => $merchant->id,
                'plan_id' => Plan::query()->where('code', 'starter_monthly')->value('id'),
                'status' => 'activated',
                'source' => 'manual',
                'plan_name' => '基础版（月付）',
                'billing_cycle' => 'monthly',
                'period_months' => 1,
                'amount' => 99,
                'starts_at' => now()->subDays(20),
                'ends_at' => now()->addDays(10),
                'activated_at' => now()->subDays(20),
                'snapshot' => ['features' => ['基础功能']],
            ]
        );

        PlatformOrder::query()->updateOrCreate(
            ['order_no' => 'PO202603100001'],
            [
                'merchant_id' => $merchant->id,
                'plan_id' => $subscription->plan_id,
                'site_subscription_id' => $subscription->id,
                'created_by_admin_id' => $platformAdmin->id,
                'order_type' => 'new_purchase',
                'status' => 'activated',
                'payment_status' => 'paid',
                'payment_channel' => 'manual',
                'plan_name' => '基础版（月付）',
                'billing_cycle' => 'monthly',
                'period_months' => 1,
                'quantity' => 1,
                'list_amount' => 129,
                'discount_amount' => 30,
                'payable_amount' => 99,
                'paid_amount' => 99,
                'placed_at' => now()->subDays(21),
                'paid_at' => now()->subDays(20),
                'activated_at' => now()->subDays(20),
                'plan_snapshot' => ['name' => '基础版（月付）', 'cycle' => 'monthly'],
                'meta' => ['note' => '初始化演示订单'],
            ]
        );

        $secondProduct = Product::query()->firstOrCreate(
            ['sku' => 'SKU-DEMO-002'],
            [
                'merchant_id' => $merchant->id,
                'category_id' => $featuredCategory->id,
                'title' => '精选礼包',
                'slug' => 'featured-bundle',
                'summary' => '用于验证分类和订单明细的第二个演示商品。',
                'content' => '后续可以扩展为组合商品、营销礼包、加价购等场景。',
                'price' => 99.00,
                'original_price' => 129.00,
                'stock' => 50,
                'status' => 'published',
                'images' => ['https://via.placeholder.com/600x600.png?text=Featured+Bundle'],
            ]
        );

        $orders = [
            [
                'order_no' => 'ORD202603080001',
                'status' => 'pending',
                'platform' => 'pc',
                'payment_channel' => 'wechat_pay',
                'payment_status' => 'unpaid',
                'device_type' => 'desktop',
                'product_amount' => 397.00,
                'discount_amount' => 0,
                'shipping_amount' => 0,
                'pay_amount' => 397.00,
                'remark' => '初始化演示订单',
                'items' => [
                    ['product' => $product, 'price' => 199.00, 'quantity' => 1, 'category' => $defaultCategory->name],
                    ['product' => $secondProduct, 'price' => 99.00, 'quantity' => 2, 'category' => $featuredCategory->name],
                ],
            ],
            [
                'order_no' => 'ORD202603080002',
                'status' => 'paid',
                'platform' => 'h5',
                'payment_channel' => 'alipay',
                'payment_status' => 'paid',
                'device_type' => 'mobile',
                'product_amount' => 99.00,
                'discount_amount' => 0,
                'shipping_amount' => 0,
                'pay_amount' => 99.00,
                'remark' => '已支付待发货演示订单',
                'items' => [
                    ['product' => $secondProduct, 'price' => 99.00, 'quantity' => 1, 'category' => $featuredCategory->name],
                ],
            ],
            [
                'order_no' => 'ORD202603080003',
                'status' => 'shipped',
                'platform' => 'wechat_mini',
                'payment_channel' => 'wechat_pay',
                'payment_status' => 'paid',
                'device_type' => 'mini-program',
                'product_amount' => 199.00,
                'discount_amount' => 10.00,
                'shipping_amount' => 0,
                'pay_amount' => 189.00,
                'remark' => '已发货演示订单',
                'items' => [
                    ['product' => $product, 'price' => 199.00, 'quantity' => 1, 'category' => $defaultCategory->name],
                ],
            ],
            [
                'order_no' => 'ORD202603080004',
                'status' => 'completed',
                'platform' => 'wechat_mp',
                'payment_channel' => 'wechat_pay',
                'payment_status' => 'paid',
                'device_type' => 'mobile-webview',
                'product_amount' => 99.00,
                'discount_amount' => 0,
                'shipping_amount' => 0,
                'pay_amount' => 99.00,
                'remark' => '已完成演示订单',
                'items' => [
                    ['product' => $secondProduct, 'price' => 99.00, 'quantity' => 1, 'category' => $featuredCategory->name],
                ],
            ],
            [
                'order_no' => 'ORD202603080005',
                'status' => 'cancelled',
                'platform' => 'app',
                'payment_channel' => 'wechat_pay',
                'payment_status' => 'failed',
                'device_type' => 'app-api',
                'product_amount' => 199.00,
                'discount_amount' => 0,
                'shipping_amount' => 0,
                'pay_amount' => 199.00,
                'remark' => '已取消演示订单',
                'items' => [
                    ['product' => $product, 'price' => 199.00, 'quantity' => 1, 'category' => $defaultCategory->name],
                ],
            ],
        ];

        foreach ($orders as $index => $orderData) {
            $order = Order::query()->updateOrCreate(
                ['order_no' => $orderData['order_no']],
                [
                    'merchant_id' => $merchant->id,
                    'user_id' => $user->id,
                    'status' => $orderData['status'],
                    'platform' => $orderData['platform'],
                    'payment_channel' => $orderData['payment_channel'],
                    'payment_status' => $orderData['payment_status'],
                    'device_type' => $orderData['device_type'],
                    'product_amount' => $orderData['product_amount'],
                    'discount_amount' => $orderData['discount_amount'],
                    'shipping_amount' => $orderData['shipping_amount'],
                    'pay_amount' => $orderData['pay_amount'],
                    'buyer_name' => $user->name,
                    'buyer_phone' => $user->phone,
                    'buyer_email' => $user->email,
                    'remark' => $orderData['remark'],
                    'paid_at' => in_array($orderData['payment_status'], ['paid', 'refunded'], true) ? now()->subDays(5 - $index) : null,
                    'shipped_at' => in_array($orderData['status'], ['shipped', 'completed'], true) ? now()->subDays(3 - min($index, 2)) : null,
                    'completed_at' => $orderData['status'] === 'completed' ? now()->subDay() : null,
                    'created_at' => now()->subDays(6 - $index),
                    'updated_at' => now()->subDays(max(0, 5 - $index)),
                ]
            );

            foreach ($orderData['items'] as $item) {
                OrderItem::query()->updateOrCreate(
                    ['order_id' => $order->id, 'product_id' => $item['product']->id],
                    [
                        'merchant_id' => $merchant->id,
                        'product_title' => $item['product']->title,
                        'product_sku' => $item['product']->sku,
                        'product_price' => $item['price'],
                        'quantity' => $item['quantity'],
                        'line_total_amount' => $item['price'] * $item['quantity'],
                        'snapshot' => [
                            'category' => $item['category'],
                            'price' => number_format($item['price'], 2, '.', ''),
                        ],
                    ]
                );
            }
        }
    }

    protected function seedPlatformConfigs(): void
    {
        $systemConfigs = [
            ['config_key' => 'platform_name', 'config_name' => '平台名称', 'config_value' => 'SaaSShop', 'value_type' => 'string', 'autoload' => true, 'group' => 'system', 'remark' => '平台展示名称'],
            ['config_key' => 'default_currency', 'config_name' => '默认货币', 'config_value' => 'CNY', 'value_type' => 'string', 'autoload' => true, 'group' => 'system', 'remark' => '全局默认结算货币'],
            ['config_key' => 'default_locale', 'config_name' => '默认语言', 'config_value' => 'zh-CN', 'value_type' => 'string', 'autoload' => true, 'group' => 'system', 'remark' => '后台默认语言'],
            ['config_key' => 'merchant_mode', 'config_name' => '商家模式', 'config_value' => 'multi-merchant', 'value_type' => 'string', 'autoload' => true, 'group' => 'merchant', 'remark' => '当前系统采用多商家平台模式'],
            ['config_key' => 'feature_order_auto_close', 'config_name' => '订单自动关闭开关', 'config_value' => '1', 'value_type' => 'boolean', 'autoload' => true, 'group' => 'feature', 'remark' => '用于演示布尔型配置编辑'],
            ['config_key' => 'default_order_auto_close_minutes', 'config_name' => '默认订单关闭分钟数', 'config_value' => '30', 'value_type' => 'number', 'autoload' => true, 'group' => 'feature', 'remark' => '用于演示数值型配置编辑'],
            ['config_key' => 'platform_theme_tokens', 'config_name' => '平台主题 Tokens', 'config_value' => '{"primary":"#2563eb","success":"#059669"}', 'value_type' => 'json', 'autoload' => true, 'group' => 'theme', 'remark' => '用于演示 JSON 型配置编辑'],
            ['config_key' => 'app_api_status', 'config_name' => 'APP API 状态', 'config_value' => 'reserved', 'value_type' => 'string', 'autoload' => true, 'group' => 'channel', 'remark' => 'APP 前端暂缓，统一 API 已预留'],
            ['config_key' => 'wechat_mp_status', 'config_name' => '公众号状态', 'config_value' => 'placeholder', 'value_type' => 'string', 'autoload' => true, 'group' => 'channel', 'remark' => '公众号能力占位中'],
            ['config_key' => 'wechat_mini_status', 'config_name' => '小程序状态', 'config_value' => 'placeholder', 'value_type' => 'string', 'autoload' => true, 'group' => 'channel', 'remark' => '小程序能力占位中'],
        ];

        foreach ($systemConfigs as $config) {
            SystemConfig::query()->updateOrCreate(
                ['config_key' => $config['config_key']],
                $config
            );
        }

        $channelConfigs = [
            ['channel_code' => 'pc', 'channel_name' => 'PC 商城', 'channel_type' => 'sales', 'status' => 'enabled', 'entry_path' => '/pc', 'supports_login' => true, 'supports_payment' => true, 'supports_share' => true, 'sort' => 10, 'settings' => ['device' => 'desktop'], 'remark' => '面向桌面浏览器的标准商城入口'],
            ['channel_code' => 'h5', 'channel_name' => 'H5 商城', 'channel_type' => 'sales', 'status' => 'enabled', 'entry_path' => '/h5', 'supports_login' => true, 'supports_payment' => true, 'supports_share' => true, 'sort' => 20, 'settings' => ['device' => 'mobile-web'], 'remark' => '面向移动浏览器和分享链路'],
            ['channel_code' => 'wechat_mp', 'channel_name' => '微信公众号', 'channel_type' => 'wechat', 'status' => 'reserved', 'entry_path' => '/wechat/mp', 'supports_login' => true, 'supports_payment' => true, 'supports_share' => true, 'sort' => 30, 'settings' => ['scene' => 'official-account'], 'remark' => '已预留占位接口，待接入微信能力'],
            ['channel_code' => 'wechat_mini', 'channel_name' => '微信小程序', 'channel_type' => 'wechat', 'status' => 'reserved', 'entry_path' => '/wechat/mini', 'supports_login' => true, 'supports_payment' => true, 'supports_share' => true, 'sort' => 40, 'settings' => ['scene' => 'mini-program'], 'remark' => '已预留占位接口，待接入微信能力'],
            ['channel_code' => 'app_api', 'channel_name' => 'APP API', 'channel_type' => 'api', 'status' => 'reserved', 'entry_path' => '/api/v1', 'supports_login' => true, 'supports_payment' => true, 'supports_share' => false, 'sort' => 50, 'settings' => ['scene' => 'mobile-app-api'], 'remark' => '当前不做 APP 前端，但统一 API 已预留'],
        ];

        foreach ($channelConfigs as $channel) {
            ChannelConfig::query()->updateOrCreate(
                ['channel_code' => $channel['channel_code']],
                $channel
            );
        }

        $paymentConfigs = [
            ['payment_code' => 'wechat_pay', 'payment_name' => '微信支付', 'provider' => 'wechat', 'status' => 'reserved', 'is_sandbox' => true, 'supports_refund' => true, 'settings' => ['mode' => 'service-provider'], 'remark' => '平台级微信支付配置预留'],
            ['payment_code' => 'alipay', 'payment_name' => '支付宝', 'provider' => 'alipay', 'status' => 'reserved', 'is_sandbox' => true, 'supports_refund' => true, 'settings' => ['mode' => 'standard'], 'remark' => '平台级支付宝配置预留'],
        ];

        foreach ($paymentConfigs as $payment) {
            PaymentConfig::query()->updateOrCreate(
                ['payment_code' => $payment['payment_code']],
                $payment
            );
        }
    }
}
