<?php

namespace Database\Seeders;

use App\Domains\Analytics\Models\Click;
use App\Domains\Link\Models\Link;
use App\Domains\Workspace\Models\Workspace;
use App\Domains\Workspace\Models\WorkspaceMember;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class DemoDataSeeder extends Seeder
{
    public function run(): void
    {
        $user = User::where('email', 'nexxo@nimuli.com')->firstOrFail();

        $workspace = Workspace::firstOrCreate(
            ['owner_id' => $user->id],
            [
                'name' => 'Demo Agency',
                'slug' => 'demo-agency',
            ]
        );

        // Ensure owner has a membership record and default workspace set
        WorkspaceMember::firstOrCreate(
            ['workspace_id' => $workspace->id, 'user_id' => $user->id],
            ['role' => 'owner', 'joined_at' => now()]
        );
        $user->updateQuietly(['default_workspace_id' => $workspace->id]);

        $this->command->info("Workspace: {$workspace->name} ({$workspace->ulid})");

        $slugs = ['nimuli', 'get-started', 'pricing', 'blog', 'docs', 'github', 'twitter', 'linkedin', 'youtube', 'discord', 'product', 'api', 'signup', 'login-link', 'partner', 'careers', 'press', 'legal', 'status', 'support'];
        $titles = ['Nimuli Homepage', 'Get Started Guide', 'Pricing Page', 'Blog', 'Documentation', 'GitHub Repo', 'Twitter/X', 'LinkedIn', 'YouTube Channel', 'Discord Community', 'Product Page', 'API Docs', 'Sign Up', 'Login Link', 'Partner Program', 'Careers', 'Press Kit', 'Legal', 'Status Page', 'Support'];
        $targets = ['https://nimuli.com', 'https://docs.nimuli.com/start', 'https://nimuli.com/pricing', 'https://blog.nimuli.com', 'https://docs.nimuli.com', 'https://github.com/nimuli', 'https://twitter.com/nimuli', 'https://linkedin.com/company/nimuli', 'https://youtube.com/@nimuli', 'https://discord.gg/nimuli', 'https://nimuli.com/product', 'https://api.nimuli.com', 'https://app.nimuli.com/register', 'https://app.nimuli.com/login', 'https://nimuli.com/partners', 'https://nimuli.com/careers', 'https://nimuli.com/press', 'https://nimuli.com/legal', 'https://status.nimuli.com', 'https://support.nimuli.com'];

        $links = [];
        foreach ($slugs as $i => $slug) {
            $link = Link::firstOrCreate(
                ['workspace_id' => $workspace->id, 'slug' => $slug],
                [
                    'title' => $titles[$i],
                    'target_url' => $targets[$i],
                    'status' => $i < 18 ? 'active' : ($i === 18 ? 'disabled' : 'active'),
                    'tags' => $i < 5 ? ['marketing'] : ($i < 10 ? ['social'] : ['product']),
                ]
            );
            $links[] = $link;
        }

        $this->command->info('Created '.count($links).' links');

        $countries = ['DE', 'US', 'GB', 'FR', 'AT', 'CH', 'NL', 'PL', 'IT', 'ES'];
        $devices = ['desktop', 'mobile', 'tablet'];
        $browsers = ['Chrome', 'Firefox', 'Safari', 'Edge'];
        $oses = ['Windows', 'macOS', 'Linux', 'iOS', 'Android'];

        $clickCount = 0;
        foreach ($links as $link) {
            $baseClicks = random_int(10, 80);
            for ($j = 0; $j < $baseClicks; $j++) {
                $daysAgo = random_int(0, 29);
                $hoursAgo = random_int(0, 23);
                $clickedAt = now()->subDays($daysAgo)->subHours($hoursAgo);

                Click::create([
                    'link_id' => $link->id,
                    'workspace_id' => $workspace->id,
                    'clicked_at' => $clickedAt,
                    'ip_hash' => hash('sha256', Str::random(16)),
                    'country' => $countries[array_rand($countries)],
                    'city' => null,
                    'device' => $devices[array_rand($devices)],
                    'os' => $oses[array_rand($oses)],
                    'browser' => $browsers[array_rand($browsers)],
                    'referrer_host' => null,
                ]);
                $clickCount++;
            }
        }

        $this->command->info("Created {$clickCount} clicks over 30 days");
        $this->command->info("Dashboard URL: /w/{$workspace->ulid}/links");
    }
}
