<?php

namespace Database\Seeders;

use App\Models\AiRequest;
use App\Models\License;
use App\Models\LicenseActivation;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Admin user
        User::create([
            'first_name' => 'Admin',
            'last_name' => 'User',
            'email' => 'admin@storesight.ai',
            'password' => Hash::make('password'),
            'is_admin' => true,
        ]);

        // Sample users with licenses and activations
        $usersData = [
            ['first_name' => 'John', 'last_name' => 'Doe', 'email' => 'john@example.com', 'domains' => ['myshop.example.com']],
            ['first_name' => 'Sarah', 'last_name' => 'Mitchell', 'email' => 'sarah@shopify-migration.co', 'domains' => ['fashion-hub.co.uk']],
            ['first_name' => 'Alex', 'last_name' => 'Kim', 'email' => 'alex@digitalstore.io', 'domains' => ['gadgets-direct.com']],
            ['first_name' => 'Rachel', 'last_name' => 'Park', 'email' => 'rachel@boutique-online.com', 'domains' => ['boutique-online.com']],
            ['first_name' => 'Tom', 'last_name' => 'Wilson', 'email' => 'tom@wilsonretail.co.uk', 'domains' => ['wilsonretail.co.uk']],
        ];

        $prompts = [
            ['Show revenue for last 3 months', 'widget'],
            ['Top 5 products this week', 'assistant'],
            ['Generate customer growth report', 'report'],
            ['Customer orders this month', 'widget'],
            ['Compare January and February revenue', 'assistant'],
            ['Revenue breakdown by category', 'report'],
            ['What are my best selling items?', 'assistant'],
            ['Weekly sales summary', 'widget'],
        ];

        foreach ($usersData as $index => $userData) {
            $domains = $userData['domains'];
            unset($userData['domains']);

            $user = User::create(array_merge($userData, [
                'password' => Hash::make('password'),
                'created_at' => now()->subDays(rand(10, 120)),
            ]));

            // Active license with activation
            $license = License::create([
                'user_id' => $user->id,
                'status' => $index < 4 ? 'active' : 'inactive',
                'max_sites' => 1,
                'created_at' => $user->created_at->addDays(rand(0, 5)),
            ]);

            if ($license->status === 'active') {
                $activation = LicenseActivation::create([
                    'license_id' => $license->id,
                    'domain' => $domains[0],
                    'status' => $index < 3 ? 'active' : 'inactive',
                    'activated_at' => $license->created_at->addDay(),
                    'last_seen_at' => now()->subMinutes(rand(1, 4320)),
                ]);

                // Generate AI requests spread across the last week
                for ($j = 0; $j < rand(10, 30); $j++) {
                    $prompt = $prompts[array_rand($prompts)];
                    AiRequest::create([
                        'license_activation_id' => $activation->id,
                        'domain' => $activation->domain,
                        'prompt' => $prompt[0],
                        'response' => 'Sample AI response for: ' . $prompt[0],
                        'request_type' => $prompt[1],
                        'tokens_used' => rand(200, 900),
                        'response_time_ms' => rand(800, 2500),
                        'created_at' => now()->subHours(rand(0, 168)),
                    ]);
                }
            }

            // Extra inactive license for first user
            if ($index === 0) {
                License::create([
                    'user_id' => $user->id,
                    'status' => 'inactive',
                    'max_sites' => 1,
                ]);
            }
        }

        // Default settings
        $defaults = [
            'openai_model' => 'gpt-5-mini',
            'openai_max_tokens' => '1024',
            'rate_limit_per_day' => '200',
            'rate_limit_per_minute' => '10',
            'max_tokens_per_day' => '500000',
            'license_price' => '29',
            'free_trial_days' => '14',
            'sites_per_license' => '1',
            'token_expiry_days' => '30',
            'heartbeat_enabled' => '1',
            'admin_email' => 'admin@storesight.ai',
        ];

        foreach ($defaults as $key => $value) {
            Setting::set($key, $value);
        }
    }
}
