<?php

namespace Database\Seeders;

use App\Domain\Account\Enums\AccountStatus;
use App\Domain\Account\Models\Account;
use App\Domain\Backup\Models\BackupDestination;
use App\Domain\Backup\Models\BackupSchedule;
use App\Domain\Database\Enums\DatabaseStatus;
use App\Domain\Database\Models\MysqlDatabase;
use App\Domain\Database\Models\MysqlUser;
use App\Domain\Domain\Enums\DomainStatus;
use App\Domain\Domain\Enums\DomainType;
use App\Domain\Domain\Models\Domain;
use App\Domain\Email\Models\EmailAccount;
use App\Domain\Notifications\Models\PanelNotification;
use App\Domain\Package\Models\Package;
use App\Domain\Reseller\Models\ResellerLimit;
use App\Domain\Snapshot\Models\AccountSnapshot;
// SupportTicket modelleri kaldırıldı (WHM-style — eklenti entegrasyonu için)
use App\Domain\Webdisk\Models\WebdiskAccount;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
    /**
     * Phase 1 Epic 1 seeding:
     * 1. Roller + izinler
     * 2. Default paketler (Lite/Pro/Unlimited/Enterprise)
     * 3. Admin user
     * 4. Demo end-user users + onların hesapları
     */
    public function run(): void
    {
        $this->call([
            RolesAndPermissionsSeeder::class,
            PackageSeeder::class,
            DnsTemplateSeeder::class,
        ]);

        if (! app()->environment(['local', 'staging'])) {
            return;
        }

        // ---- Admin (panel sahibi) ----
        $admin = User::firstOrCreate(
            ['email' => 'admin@onoxsoft.local'],
            [
                'name' => 'ONOXSOFT Admin',
                'password' => bcrypt('password'),
                'email_verified_at' => now(),
            ]
        );
        if (! $admin->hasRole('admin')) {
            $admin->assignRole('admin');
        }

        // Demo notifications for admin
        if (PanelNotification::where('user_id', $admin->id)->count() === 0) {
            PanelNotification::create([
                'user_id' => $admin->id, 'category' => 'security', 'priority' => 'high',
                'title' => 'Yeni admin girişi',
                'body' => 'Hesabınıza Ankara/TR\'den giriş yapıldı. Siz değilseniz parolanızı değiştirin.',
                'icon' => 'ShieldCheckIcon',
                'created_at' => now()->subHours(2), 'updated_at' => now()->subHours(2),
            ]);
            PanelNotification::create([
                'user_id' => $admin->id, 'category' => 'warning', 'priority' => 'high',
                'title' => 'SSL sertifikası süresi yaklaşıyor',
                'body' => 'acme.com.tr SSL sertifikası 12 gün içinde sona erecek. Otomatik yenileme aktif.',
                'icon' => 'ExclamationTriangleIcon',
                'link' => '/admin/health',
                'created_at' => now()->subDay(), 'updated_at' => now()->subDay(),
            ]);
            PanelNotification::create([
                'user_id' => $admin->id, 'category' => 'success', 'priority' => 'normal',
                'title' => '5 yeni hesap oluşturuldu',
                'body' => 'Son 24 saatte API üzerinden 5 yeni Linux hesabı provisioned edildi.',
                'icon' => 'CheckCircleIcon',
                'link' => '/admin/accounts',
                'read_at' => now()->subHours(3),
                'created_at' => now()->subHours(5), 'updated_at' => now()->subHours(3),
            ]);
            PanelNotification::create([
                'user_id' => $admin->id, 'category' => 'info', 'priority' => 'low',
                'title' => 'Yeni panel sürümü mevcut',
                'body' => 'ONOXSOFT v0.2.0-beta yayınlandı. Güncelleme notlarını incelemek için tıklayın.',
                'icon' => 'InformationCircleIcon',
                'created_at' => now()->subDays(3), 'updated_at' => now()->subDays(3),
            ]);
        }

        if (Account::count() > 0) {
            return; // Idempotent
        }

        // ---- Backup destinations + schedules ----
        if (BackupDestination::count() === 0) {
            $localDest = BackupDestination::create([
                'name' => 'Yerel Disk',
                'driver' => 'local',
                'is_active' => true,
                'is_default' => true,
                'local_path' => '/var/backups/onox',
                'total_bytes' => 1024 * 1024 * 1024 * 18,
                'backup_count' => 124,
                'last_backup_at' => now()->subHours(3),
                'last_health_check_at' => now()->subMinutes(15),
                'is_healthy' => true,
            ]);

            $s3Dest = BackupDestination::create([
                'name' => 'AWS S3 (Off-site)',
                'driver' => 's3',
                'is_active' => true,
                'is_default' => false,
                'bucket' => 'onox-backups-eu',
                'region' => 'eu-central-1',
                'access_key' => 'AKIAIOSFODNN7EXAMPLE',
                'secret_key' => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
                'path_prefix' => 'server1/',
                'total_bytes' => 1024 * 1024 * 1024 * 47,
                'backup_count' => 89,
                'last_backup_at' => now()->subHours(8),
                'last_health_check_at' => now()->subMinutes(30),
                'is_healthy' => true,
            ]);

            BackupSchedule::create([
                'name' => 'Günlük Tam Yedek',
                'is_active' => true,
                'cron' => '0 4 * * *',
                'scope' => 'full',
                'destination_id' => $localDest->id,
                'compression' => 'gzip',
                'encrypt' => false,
                'keep_daily' => 7, 'keep_weekly' => 4, 'keep_monthly' => 6, 'keep_yearly' => 2,
                'exclude_paths' => ['node_modules', 'tmp', 'cache', '.git'],
                'last_run_at' => now()->subHours(3),
                'last_run_status' => 'success',
                'last_run_duration_seconds' => 187,
                'last_run_size_bytes' => 1024 * 1024 * 850,
                'next_run_at' => now()->addHours(21),
            ]);

            BackupSchedule::create([
                'name' => 'Haftalık Off-site (S3)',
                'is_active' => true,
                'cron' => '0 3 * * 0',
                'scope' => 'full',
                'destination_id' => $s3Dest->id,
                'compression' => 'zstd',
                'encrypt' => true,
                'keep_daily' => 0, 'keep_weekly' => 8, 'keep_monthly' => 6, 'keep_yearly' => 2,
                'last_run_at' => now()->subDays(3),
                'last_run_status' => 'success',
                'next_run_at' => now()->next(Carbon::SUNDAY)->setTime(3, 0),
            ]);

            BackupSchedule::create([
                'name' => 'Saatlik DB Snapshot',
                'is_active' => false,
                'cron' => '0 */6 * * *',
                'scope' => 'db_only',
                'destination_id' => $localDest->id,
                'compression' => 'gzip',
                'encrypt' => false,
                'keep_daily' => 3, 'keep_weekly' => 0, 'keep_monthly' => 0, 'keep_yearly' => 0,
                'next_run_at' => now()->addHours(2),
            ]);
        }

        // ---- Demo Reseller (bayi) ----
        $reseller = User::firstOrCreate(
            ['email' => 'bayi@ornekhosting.com'],
            [
                'name' => 'Örnek Hosting Bayisi',
                'password' => bcrypt('password'),
                'email_verified_at' => now(),
            ]
        );
        if (! $reseller->hasRole('reseller')) {
            $reseller->assignRole('reseller');
        }
        ResellerLimit::firstOrCreate(
            ['user_id' => $reseller->id],
            [
                'max_accounts' => 50,
                'max_disk_mb' => 51200,
                'max_bandwidth_mb_month' => 500_000,
                'max_databases' => 100,
                'max_email_accounts' => 200,
                'max_domains' => 100,
                'max_subdomains' => 500,
                'max_ftp_accounts' => 100,
                'allowed_package_ids' => null,
                'white_label_enabled' => true,
                'white_label_brand_name' => 'ÖrnekHosting',
                'white_label_primary_color' => '#0EA5E9',
                'white_label_support_email' => 'destek@ornekhosting.com',
                'white_label_panel_domain' => 'panel.ornekhosting.com',
                'permissions' => array_merge(
                    ResellerLimit::defaultPermissions(),
                    ['manage_packages', 'view_audit'],
                ),
                'is_active' => true,
            ]
        );

        // Reseller demo audit logs
        foreach ([
            ['action' => 'reseller.login', 'subHours' => 1],
            ['action' => 'account.created', 'subHours' => 24, 'target' => 'Account', 'tid' => 2],
            ['action' => 'domain.added', 'subHours' => 48, 'target' => 'Domain', 'tid' => 3],
            ['action' => 'account.suspended', 'subHours' => 72, 'target' => 'Account', 'tid' => 4],
            ['action' => 'ssl.renewed', 'subHours' => 96],
            ['action' => 'database.created', 'subHours' => 120, 'target' => 'MysqlDatabase', 'tid' => 5],
            ['action' => 'email.account.created', 'subHours' => 144, 'target' => 'EmailAccount', 'tid' => 1],
        ] as $i => $log) {
            DB::table('audit_log')->insert([
                'user_id' => $reseller->id,
                'ip_address' => '192.0.2.'.(40 + $i),
                'action' => $log['action'],
                'target_type' => $log['target'] ?? null,
                'target_id' => $log['tid'] ?? null,
                'outcome' => 'success',
                'created_at' => Carbon::now()->subHours($log['subHours']),
            ]);
        }

        $medium = Package::where('slug', 'medium')->first();
        $small = Package::where('slug', 'small')->first();
        $large = Package::where('slug', 'large')->first();
        $unlimited = Package::where('slug', 'unlimited')->first();

        // ---- Demo end-user customers + her birinin kendi hesabı ----
        $demos = [
            [
                'user_email' => 'ahmet@acme.com.tr',
                'user_name' => 'Ahmet Yılmaz',
                'username' => 'onx_acme01',
                'domain' => 'acme.com.tr',
                'package' => $medium,
                'status' => AccountStatus::Active,
                'uid' => 10001,
                'disk' => 65,
            ],
            [
                'user_email' => 'orhan@orhanblog.com',
                'user_name' => 'Orhan Demir',
                'username' => 'onx_blog42',
                'domain' => 'orhanblog.com',
                'package' => $small,
                'status' => AccountStatus::Active,
                'uid' => 10002,
                'disk' => 22,
                'reseller_managed' => true,
            ],
            [
                'user_email' => 'mehmet@modashop.com.tr',
                'user_name' => 'Mehmet Kaya',
                'username' => 'onx_shop99',
                'domain' => 'modashop.com.tr',
                'package' => $large,
                'status' => AccountStatus::Active,
                'uid' => 10003,
                'disk' => 78,
                'reseller_managed' => true,
            ],
            [
                'user_email' => 'dev@yazilim.io',
                'user_name' => 'Burak Çelik',
                'username' => 'onx_dev123',
                'domain' => 'yazilim.io',
                'package' => $medium,
                'status' => AccountStatus::Suspended,
                'uid' => 10004,
                'disk' => 45,
                'suspend_reason' => 'Ödeme gecikmesi (test)',
            ],
            [
                'user_email' => 'test@example.com',
                'user_name' => 'Test Kullanıcı',
                'username' => 'onx_test01',
                'domain' => null,
                'package' => $small,
                'status' => AccountStatus::Pending,
                'uid' => null,
                'disk' => 0,
            ],
        ];

        foreach ($demos as $d) {
            $user = User::firstOrCreate(
                ['email' => $d['user_email']],
                [
                    'name' => $d['user_name'],
                    'password' => bcrypt('password'),
                    'email_verified_at' => now(),
                ]
            );
            if (! $user->hasRole('user')) {
                $user->assignRole('user');
            }

            // Demo notifications for first user
            if ($d['username'] === 'onx_acme01' && PanelNotification::where('user_id', $user->id)->count() === 0) {
                PanelNotification::create([
                    'user_id' => $user->id, 'category' => 'success', 'priority' => 'normal',
                    'title' => 'Hoş geldiniz!',
                    'body' => 'ONOXSOFT panele hoş geldiniz. İlk uygulamanızı katalogdan kurabilirsiniz.',
                    'icon' => 'CheckCircleIcon',
                    'link' => '/panel/apps',
                    'created_at' => now()->subDays(7), 'updated_at' => now()->subDays(7),
                ]);
                PanelNotification::create([
                    'user_id' => $user->id, 'category' => 'success', 'priority' => 'normal',
                    'title' => 'Yedek başarıyla alındı',
                    'body' => 'Hesabınızın günlük yedeği tamamlandı (245 MB).',
                    'icon' => 'CheckCircleIcon',
                    'link' => '/panel/backups',
                    'read_at' => now()->subHours(8),
                    'created_at' => now()->subHours(10), 'updated_at' => now()->subHours(8),
                ]);
                PanelNotification::create([
                    'user_id' => $user->id, 'category' => 'warning', 'priority' => 'high',
                    'title' => 'SSL sertifikanız yakında yenilenecek',
                    'body' => 'acme.com.tr için SSL sertifikası 14 gün sonra otomatik yenilenecek.',
                    'icon' => 'ShieldCheckIcon',
                    'link' => '/panel/ssl',
                    'created_at' => now()->subHours(4), 'updated_at' => now()->subHours(4),
                ]);
            }

            $quota = $d['package']->limit('disk_mb') ? $d['package']->limit('disk_mb') * 1024 * 1024 : 5 * 1024 * 1024 * 1024;

            // Reseller-managed accounts → owner_user_id = reseller
            $ownerId = ($d['reseller_managed'] ?? false) ? $reseller->id : $user->id;

            $account = Account::create([
                'owner_user_id' => $ownerId,
                'package_id' => $d['package']->id,
                'username' => $d['username'],
                'linux_uid' => $d['uid'],
                'email' => $d['user_email'],
                'primary_domain' => $d['domain'],
                'status' => $d['status'],
                'quota_bytes' => $quota,
                'disk_usage_bytes' => (int) ($quota * $d['disk'] / 100),
                'provisioned_at' => $d['status'] !== AccountStatus::Pending ? now()->subDays(rand(10, 200)) : null,
                'suspended_at' => $d['status'] === AccountStatus::Suspended ? now()->subDays(rand(1, 30)) : null,
                'suspended_reason' => $d['suspend_reason'] ?? null,
                'usage_updated_at' => now(),
            ]);

            // Main domain
            if ($d['status'] === AccountStatus::Active && $d['domain']) {
                $main = Domain::create([
                    'account_id' => $account->id,
                    'name' => $d['domain'],
                    'type' => DomainType::Main,
                    'document_root' => "/home/{$d['username']}/public_html",
                    'php_version' => '8.2',
                    'ssl_enabled' => true,
                    'force_https' => true,
                    'status' => DomainStatus::Active,
                    'last_provisioned_at' => now()->subDays(rand(1, 100)),
                ]);

                // www. parked
                Domain::create([
                    'account_id' => $account->id,
                    'name' => 'www.'.$d['domain'],
                    'type' => DomainType::Parked,
                    'parent_domain_id' => $main->id,
                    'document_root' => $main->document_root,
                    'php_version' => '8.2',
                    'ssl_enabled' => true,
                    'status' => DomainStatus::Active,
                ]);

                // Random subdomain
                if (rand(0, 1)) {
                    Domain::create([
                        'account_id' => $account->id,
                        'name' => 'blog.'.$d['domain'],
                        'type' => DomainType::Subdomain,
                        'parent_domain_id' => $main->id,
                        'document_root' => "/home/{$d['username']}/public_html/blog",
                        'php_version' => '8.2',
                        'ssl_enabled' => true,
                        'status' => DomainStatus::Active,
                    ]);
                }

                // Demo MySQL DB + user + grant
                $dbNames = ['wp_main', 'staging'];
                foreach (array_slice($dbNames, 0, rand(1, 2)) as $dbName) {
                    $db = MysqlDatabase::create([
                        'account_id' => $account->id,
                        'name' => $dbName,
                        'real_db_name' => $account->username.'_'.$dbName,
                        'charset' => 'utf8mb4',
                        'collation' => 'utf8mb4_unicode_ci',
                        'status' => DatabaseStatus::Active,
                        'size_bytes' => rand(1024 * 100, 1024 * 1024 * 250),
                        'last_provisioned_at' => now()->subDays(rand(1, 90)),
                        'size_updated_at' => now()->subHours(rand(1, 24)),
                    ]);

                    $dbUser = MysqlUser::firstOrCreate(
                        ['account_id' => $account->id, 'name' => 'dbuser'],
                        [
                            'real_user_name' => $account->username.'_dbuser',
                            'host' => 'localhost',
                            'auth_plugin' => 'mysql_native_password',
                            'status' => DatabaseStatus::Active,
                            'last_provisioned_at' => now()->subDays(rand(1, 90)),
                        ],
                    );

                    DB::table('mysql_grants')->insertOrIgnore([
                        'database_id' => $db->id,
                        'user_id' => $dbUser->id,
                        'privileges' => json_encode(['ALL']),
                        'created_at' => now(),
                        'updated_at' => now(),
                    ]);
                }

                // Demo email accounts
                if (in_array($d['username'], ['onx_acme01', 'onx_shop99'], true)) {
                    $emailDomain = $d['domain'];
                    $locals = $d['username'] === 'onx_acme01'
                        ? ['info', 'destek', 'satis']
                        : ['info', 'siparis', 'iletisim', 'kargo'];
                    foreach ($locals as $local) {
                        EmailAccount::firstOrCreate(
                            ['local' => $local, 'domain' => $emailDomain],
                            [
                                'account_id' => $account->id,
                                'password_hash' => password_hash('demo123', PASSWORD_BCRYPT),
                                'quota_mb' => 1024,
                                'usage_bytes' => rand(50 * 1024 * 1024, 700 * 1024 * 1024),
                                'status' => 'active',
                            ]
                        );
                    }
                }

                // Demo installed apps (sadece bazı hesaplar için)
                if (in_array($d['username'], ['onx_acme01', 'onx_blog42', 'onx_shop99'], true)) {
                    $appType = match ($d['username']) {
                        'onx_blog42' => 'wordpress',
                        'onx_shop99' => 'woocommerce',
                        default => 'wordpress',
                    };

                    DB::table('installed_apps')->insert([
                        'account_id' => $account->id,
                        'domain_id' => $main->id,
                        'app_type' => $appType,
                        'version' => $appType === 'woocommerce' ? '8.8.0' : '6.5.3',
                        'install_url' => 'https://'.$d['domain'],
                        'install_path' => '/',
                        'database_id' => null,
                        'admin_username' => 'admin',
                        'admin_email' => $d['user_email'],
                        'admin_password_hash' => password_hash('demo123', PASSWORD_BCRYPT),
                        'site_title' => $appType === 'woocommerce' ? 'Moda Mağaza' : $d['user_name'].'\'in Sitesi',
                        'language' => 'tr_TR',
                        'config_overrides' => null,
                        'status' => 'active',
                        'installed_at' => now()->subDays(rand(7, 180)),
                        'last_checked_at' => now()->subHours(rand(1, 12)),
                        'updates_available' => rand(0, 4) === 0,
                        'latest_available_version' => $appType === 'woocommerce' ? '8.9.0' : '6.6.0',
                        'created_at' => now()->subDays(rand(7, 180)),
                        'updated_at' => now(),
                    ]);
                }

                // Demo PHP overrides (sadece dev hesabı için)
                if ($d['username'] === 'onx_dev123') {
                    foreach ([
                        ['directive' => 'memory_limit', 'value' => '512M'],
                        ['directive' => 'upload_max_filesize', 'value' => '128M'],
                        ['directive' => 'post_max_size', 'value' => '128M'],
                        ['directive' => 'max_execution_time', 'value' => '600'],
                    ] as $override) {
                        // Onun bir domain'i yok ama yine de simüle edilsin
                    }
                }

                // Demo WebDisk hesapları
                if ($d['username'] === 'onx_acme01' && WebdiskAccount::where('account_id', $account->id)->count() === 0) {
                    WebdiskAccount::create([
                        'account_id' => $account->id,
                        'username' => 'ahmet_webdisk',
                        'password_hash' => password_hash('demo123', PASSWORD_BCRYPT),
                        'home_path' => '/home/'.$account->username,
                        'permission' => 'read_write',
                        'use_digest_auth' => true,
                        'is_active' => true,
                        'last_access_at' => now()->subHours(rand(1, 48)),
                        'total_bytes_transferred' => rand(50_000_000, 500_000_000),
                    ]);
                    WebdiskAccount::create([
                        'account_id' => $account->id,
                        'username' => 'misafir_readonly',
                        'password_hash' => password_hash('demo456', PASSWORD_BCRYPT),
                        'home_path' => '/home/'.$account->username.'/public_html/images',
                        'permission' => 'read',
                        'use_digest_auth' => true,
                        'is_active' => true,
                    ]);
                }

                // Demo snapshots (acme için 2: manuel + cPanel import)
                if ($d['username'] === 'onx_acme01' && AccountSnapshot::where('account_id', $account->id)->count() === 0) {
                    AccountSnapshot::create([
                        'account_id' => $account->id,
                        'snapshot_number' => AccountSnapshot::generateNumber(),
                        'type' => 'manual_snapshot',
                        'source' => 'created',
                        'status' => 'completed',
                        'file_path' => '/var/backups/onox/snapshots/snap_onx_acme01_20260512.tar.gz',
                        'size_bytes' => 245 * 1024 * 1024,
                        'checksum_sha256' => hash('sha256', 'demo-snap-1'),
                        'includes_files' => true, 'includes_databases' => true, 'includes_dns' => true,
                        'includes_email' => true, 'includes_config' => true, 'includes_ssl' => true, 'includes_cron' => true,
                        'manifest' => [
                            'snapshot_format' => 'onox-v1',
                            'account' => ['username' => $account->username, 'primary_domain' => $d['domain']],
                            'files' => ['count' => 12340, 'size_bytes' => 230 * 1024 * 1024, 'paths' => ['/home/'.$account->username.'/public_html']],
                            'databases' => [['name' => $account->username.'_wp_main', 'size_mb' => 35, 'tables' => 28]],
                            'dns' => [['zone' => $d['domain'], 'records' => 14]],
                            'email' => ['accounts' => 3, 'forwarders' => 1],
                        ],
                        'notes' => 'WordPress 6.5 upgrade öncesi manuel snapshot',
                        'started_at' => now()->subDays(2)->subMinutes(5),
                        'completed_at' => now()->subDays(2)->subMinutes(1),
                        'duration_seconds' => 240,
                        'expires_at' => now()->addDays(28),
                        'created_by_user_id' => $admin->id,
                    ]);

                    AccountSnapshot::create([
                        'account_id' => null,
                        'snapshot_number' => AccountSnapshot::generateNumber(),
                        'type' => 'cpanel_import',
                        'source' => 'cpanel',
                        'status' => 'completed',
                        'file_path' => storage_path('app/snapshots/imports/cpmove-eskimusteri.tar.gz'),
                        'size_bytes' => (int) (1.8 * 1024 * 1024 * 1024),
                        'source_panel_version' => '110.0.16',
                        'source_hostname' => 'server47.eskihosting.com',
                        'source_username' => 'eskimusteri',
                        'includes_files' => true, 'includes_databases' => true, 'includes_dns' => true,
                        'includes_email' => true, 'includes_config' => true,
                        'manifest' => [
                            'snapshot_format' => 'cpanel-cpmove-v110',
                            'source_panel' => 'cpanel',
                            'account' => ['username' => 'eskimusteri', 'primary_domain' => 'birsite.com', 'hostname' => 'server47.eskihosting.com'],
                            'files' => ['count' => 8745, 'size_bytes' => (int) (1.5 * 1024 * 1024 * 1024), 'paths' => ['public_html/', 'mail/']],
                            'databases' => [
                                ['name' => 'eskimusteri_wp1', 'size_mb' => 85],
                                ['name' => 'eskimusteri_shop', 'size_mb' => 245],
                            ],
                            'dns_zones' => [['name' => 'birsite.com', 'records' => 18]],
                            'email_accounts' => 5,
                            'cron_jobs' => 2,
                            'compatibility_notes' => [
                                'suexec → Onoxsoft FPM pool dönüştürülecek',
                                'mod_security kuralları taşınmaz',
                                'cPanel-spesifik addons (Awstats) import edilmez',
                            ],
                        ],
                        'notes' => 'eskimusteri@eskihosting.com — müşteri Twitter\'dan başvurdu. Restore öncesi DNS NS değişikliği müşteriden istenecek.',
                        'started_at' => now()->subHours(6),
                        'completed_at' => now()->subHours(6)->addMinutes(45),
                        'duration_seconds' => 2700,
                        'created_by_user_id' => $admin->id,
                    ]);
                }

                // Demo support tickets — kaldırıldı (WHM-style, eklenti entegrasyonu için)

                // Demo error pages (sadece ilk hesap için)
                if ($d['username'] === 'onx_acme01') {
                    DB::table('error_pages')->insert([
                        [
                            'account_id' => $account->id,
                            'domain_id' => $main->id,
                            'http_code' => 404,
                            'html_content' => '<!DOCTYPE html><html lang="tr"><head><meta charset="utf-8"><title>404 — Acme</title><style>body{font-family:system-ui,sans-serif;background:linear-gradient(135deg,#6366f1,#8b5cf6);color:white;text-align:center;padding:80px 20px;min-height:100vh;margin:0}h1{font-size:120px;margin:0;text-shadow:0 4px 20px rgba(0,0,0,0.3)}p{font-size:20px}a{color:white;text-decoration:underline}</style></head><body><h1>404</h1><p>Üzgünüz, aradığınız sayfa Acme\'de bulunamadı.</p><p><a href="/">← Ana Sayfa</a></p></body></html>',
                            'created_at' => now(),
                            'updated_at' => now(),
                        ],
                        [
                            'account_id' => $account->id,
                            'domain_id' => null,
                            'http_code' => 500,
                            'html_content' => '<!DOCTYPE html><html lang="tr"><head><meta charset="utf-8"><title>500</title><style>body{font-family:system-ui;background:#f8fafc;text-align:center;padding:80px}h1{color:#ef4444;font-size:96px;margin:0}</style></head><body><h1>500</h1><p>Sunucu hatası — ekibimize bildirildi.</p></body></html>',
                            'created_at' => now(),
                            'updated_at' => now(),
                        ],
                    ]);

                    DB::table('directory_passwords')->insert([
                        'account_id' => $account->id,
                        'path' => '/admin',
                        'username' => 'admin',
                        'password_hash' => password_hash('demo123', PASSWORD_BCRYPT),
                        'realm' => 'Acme Admin Panel',
                        'is_active' => true,
                        'created_at' => now(),
                        'updated_at' => now(),
                    ]);
                }
            }
        }

        // ---- Bandwidth mock data (must run after accounts + domains exist) ----
        $this->call(BandwidthSeeder::class);
    }
}
