<?php

namespace Database\Seeders;

use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\User;
use App\Services\License\LicenseGeneratorService;
use App\Services\Security\SignatureService;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Admin user
        User::create([
            'name'              => 'Admin',
            'email'             => 'admin@licenseplatform.test',
            'password'          => Hash::make('password'),
            'is_admin'          => true,
            'is_active'         => true,
            'email_verified_at' => now(),
        ]);

        // Demo customer
        $customer = User::create([
            'name'              => 'Jane Developer',
            'email'             => 'jane@example.com',
            'password'          => Hash::make('password'),
            'company'           => 'Acme Corp',
            'is_admin'          => false,
            'is_active'         => true,
            'email_verified_at' => now(),
        ]);

        // Products
        $products = [
            [
                'name'                    => 'Hosting Manager Pro',
                'slug'                    => 'hosting-manager-pro',
                'type'                    => 'module',
                'version'                 => '2.4.1',
                'price'                   => 79.00,
                'short_description'       => 'Professional hosting management module for Perfex CRM.',
                'description'             => 'Complete hosting management solution with WHMCS integration, server monitoring, and automated billing.',
                'is_active'               => true,
                'allow_domain_activation' => true,
                'max_activations'         => null,
                'update_channel'          => 'stable',
            ],
            [
                'name'                    => 'Laravel SaaS Starter',
                'slug'                    => 'laravel-saas-starter',
                'type'                    => 'software',
                'version'                 => '1.0.0',
                'price'                   => 149.00,
                'short_description'       => 'Production-ready Laravel SaaS boilerplate.',
                'description'             => 'Build your SaaS faster with this enterprise-grade Laravel starter kit.',
                'is_active'               => true,
                'allow_domain_activation' => true,
                'max_activations'         => null,
                'update_channel'          => 'stable',
            ],
            [
                'name'                    => 'WP Security Suite',
                'slug'                    => 'wp-security-suite',
                'type'                    => 'plugin',
                'version'                 => '3.1.0',
                'price'                   => 49.00,
                'short_description'       => 'Advanced security plugin for WordPress.',
                'description'             => 'Comprehensive WordPress security: firewall, malware scanning, 2FA, and more.',
                'is_active'               => true,
                'allow_domain_activation' => true,
                'max_activations'         => null,
                'update_channel'          => 'stable',
            ],
        ];

        foreach ($products as $p) {
            Product::create($p);
        }

        // Demo order & license for customer
        $product = Product::first();

        $order = Order::create([
            'order_number'   => 'ORD-' . date('Ymd') . '-DEMO01',
            'user_id'        => $customer->id,
            'status'         => 'paid',
            'subtotal'       => $product->price,
            'tax'            => 0,
            'discount'       => 0,
            'total'          => $product->price,
            'currency'       => 'USD',
            'payment_method' => 'demo',
            'payment_id'     => 'DEMO_' . strtoupper(Str::random(10)),
            'paid_at'        => now(),
        ]);

        $item = OrderItem::create([
            'order_id'     => $order->id,
            'product_id'   => $product->id,
            'license_type' => 'single',
            'price'        => $product->price,
            'quantity'     => 1,
            'subtotal'     => $product->price,
        ]);

        app(LicenseGeneratorService::class)->generateFromOrderItem($item, $customer);

        $this->command->info('✓ Admin: admin@licenseplatform.test / password');
        $this->command->info('✓ Customer: jane@example.com / password');
        $this->command->info('✓ 3 demo products + 1 demo license created');
    }
}
