<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SettingsSeeder extends Seeder
{
    public function run(): void
    {
        $settings = [

            // ── General ──────────────────────────────────
            ['key'=>'app_name',          'value'=>'LicensePlatform',          'group'=>'general',  'type'=>'text',     'label'=>'Application Name',       'description'=>'The name displayed throughout the platform.'],
            ['key'=>'app_url',           'value'=>'http://localhost',          'group'=>'general',  'type'=>'text',     'label'=>'Application URL',        'description'=>'The base URL of your platform.'],
            ['key'=>'app_timezone',      'value'=>'UTC',                       'group'=>'general',  'type'=>'select',   'label'=>'Timezone',               'description'=>'Default timezone for dates and times.'],
            ['key'=>'default_currency',  'value'=>'USD',                       'group'=>'general',  'type'=>'select',   'label'=>'Default Currency',       'description'=>'Currency used for all transactions.'],
            ['key'=>'currency_symbol',   'value'=>'$',                         'group'=>'general',  'type'=>'text',     'label'=>'Currency Symbol',        'description'=>'Symbol displayed next to prices.'],
            ['key'=>'company_name',      'value'=>'My Company',                'group'=>'general',  'type'=>'text',     'label'=>'Company Name',           'description'=>'Your company name for invoices and emails.'],
            ['key'=>'company_email',     'value'=>'admin@example.com',         'group'=>'general',  'type'=>'text',     'label'=>'Company Email',          'description'=>'Primary contact email address.'],
            ['key'=>'company_website',   'value'=>'https://example.com',       'group'=>'general',  'type'=>'text',     'label'=>'Company Website',        'description'=>'Your company website URL.'],
            ['key'=>'maintenance_mode',  'value'=>'0',                         'group'=>'general',  'type'=>'boolean',  'label'=>'Maintenance Mode',       'description'=>'Put the platform into maintenance mode.'],
            ['key'=>'items_per_page',    'value'=>'25',                        'group'=>'general',  'type'=>'number',   'label'=>'Items Per Page',         'description'=>'Default pagination limit for admin tables.'],

            // ── Email ─────────────────────────────────────
            ['key'=>'mail_driver',       'value'=>'smtp',                      'group'=>'email',    'type'=>'select',   'label'=>'Mail Driver',            'description'=>'Email sending method.'],
            ['key'=>'mail_host',         'value'=>'smtp.mailtrap.io',          'group'=>'email',    'type'=>'text',     'label'=>'SMTP Host',              'description'=>'SMTP server hostname.'],
            ['key'=>'mail_port',         'value'=>'587',                       'group'=>'email',    'type'=>'number',   'label'=>'SMTP Port',              'description'=>'SMTP server port (587 for TLS, 465 for SSL).'],
            ['key'=>'mail_username',     'value'=>'',                          'group'=>'email',    'type'=>'text',     'label'=>'SMTP Username',          'description'=>'Your SMTP authentication username.'],
            ['key'=>'mail_password',     'value'=>'',                          'group'=>'email',    'type'=>'password', 'label'=>'SMTP Password',          'description'=>'Your SMTP authentication password.'],
            ['key'=>'mail_encryption',   'value'=>'tls',                       'group'=>'email',    'type'=>'select',   'label'=>'Encryption',             'description'=>'Email encryption method (TLS or SSL).'],
            ['key'=>'mail_from_address', 'value'=>'noreply@example.com',       'group'=>'email',    'type'=>'text',     'label'=>'From Email Address',     'description'=>'Sender email address for all outgoing emails.'],
            ['key'=>'mail_from_name',    'value'=>'',                          'group'=>'email',    'type'=>'text',     'label'=>'From Name',              'description'=>'Sender name displayed in recipient inboxes. Defaults to application name if left blank.'],

            // ── Payment ───────────────────────────────────
            ['key'=>'payment_gateway',   'value'=>'manual',                    'group'=>'payment',  'type'=>'select',   'label'=>'Active Gateway',         'description'=>'The payment gateway used for processing orders.'],
            ['key'=>'stripe_enabled',    'value'=>'0',                         'group'=>'payment',  'type'=>'boolean',  'label'=>'Enable Stripe',          'description'=>'Accept payments via Stripe.'],
            ['key'=>'stripe_public_key', 'value'=>'',                          'group'=>'payment',  'type'=>'text',     'label'=>'Stripe Public Key',      'description'=>'Your Stripe publishable API key.'],
            ['key'=>'stripe_secret_key', 'value'=>'',                          'group'=>'payment',  'type'=>'password', 'label'=>'Stripe Secret Key',      'description'=>'Your Stripe secret API key.'],
            ['key'=>'stripe_webhook',    'value'=>'',                          'group'=>'payment',  'type'=>'text',     'label'=>'Stripe Webhook Secret',  'description'=>'Webhook signing secret from Stripe dashboard.'],
            ['key'=>'paypal_enabled',    'value'=>'0',                         'group'=>'payment',  'type'=>'boolean',  'label'=>'Enable PayPal',          'description'=>'Accept payments via PayPal.'],
            ['key'=>'paypal_client_id',  'value'=>'',                          'group'=>'payment',  'type'=>'text',     'label'=>'PayPal Client ID',       'description'=>'Your PayPal application client ID.'],
            ['key'=>'paypal_secret',     'value'=>'',                          'group'=>'payment',  'type'=>'password', 'label'=>'PayPal Secret',          'description'=>'Your PayPal application secret key.'],
            ['key'=>'paypal_sandbox',    'value'=>'1',                         'group'=>'payment',  'type'=>'boolean',  'label'=>'PayPal Sandbox Mode',    'description'=>'Use PayPal sandbox for testing.'],
            ['key'=>'tax_rate',          'value'=>'0',                         'group'=>'payment',  'type'=>'number',   'label'=>'Tax Rate (%)',            'description'=>'Tax percentage applied to orders (0 to disable).'],
            ['key'=>'invoice_prefix',    'value'=>'INV-',                      'group'=>'payment',  'type'=>'text',     'label'=>'Invoice Number Prefix',  'description'=>'Prefix for generated invoice numbers.'],

            // ── License ───────────────────────────────────
            ['key'=>'trial_days',        'value'=>'14',                        'group'=>'license',  'type'=>'number',   'label'=>'Trial Period (Days)',     'description'=>'Default number of days for trial licenses.'],
            ['key'=>'license_key_prefix','value'=>'LIC',                       'group'=>'license',  'type'=>'text',     'label'=>'License Key Prefix',     'description'=>'Prefix added to generated license keys.'],
            ['key'=>'max_activations',   'value'=>'1',                         'group'=>'license',  'type'=>'number',   'label'=>'Default Max Activations','description'=>'Default activation limit for new licenses.'],
            ['key'=>'auto_suspend',      'value'=>'1',                         'group'=>'license',  'type'=>'boolean',  'label'=>'Auto-Suspend Expired',   'description'=>'Automatically suspend licenses past their expiry date.'],
            ['key'=>'expiry_warning_days','value'=>'30',                       'group'=>'license',  'type'=>'number',   'label'=>'Expiry Warning (Days)',  'description'=>'Days before expiry to send renewal reminder.'],
            ['key'=>'allow_reactivation','value'=>'1',                         'group'=>'license',  'type'=>'boolean',  'label'=>'Allow Reactivation',     'description'=>'Allow customers to reactivate deactivated installations.'],
            ['key'=>'heartbeat_interval','value'=>'24',                        'group'=>'license',  'type'=>'number',   'label'=>'Heartbeat Interval (h)', 'description'=>'How often clients should send heartbeat pings (hours).'],

            // ── Security ──────────────────────────────────
            ['key'=>'api_rate_limit',    'value'=>'60',                        'group'=>'security', 'type'=>'number',   'label'=>'API Rate Limit (req/min)','description'=>'Max API requests per IP per minute.'],
            ['key'=>'brute_force_limit', 'value'=>'10',                        'group'=>'security', 'type'=>'number',   'label'=>'Brute Force Threshold',  'description'=>'Failed attempts before IP is blocked.'],
            ['key'=>'brute_force_ttl',   'value'=>'300',                       'group'=>'security', 'type'=>'number',   'label'=>'Block Duration (sec)',   'description'=>'Seconds an IP is blocked after exceeding threshold.'],
            ['key'=>'timestamp_tolerance','value'=>'300',                      'group'=>'security', 'type'=>'number',   'label'=>'Timestamp Tolerance (s)','description'=>'Allowed timestamp drift for API request validation.'],
            ['key'=>'require_2fa',       'value'=>'0',                         'group'=>'security', 'type'=>'boolean',  'label'=>'Require 2FA for Admins', 'description'=>'Enforce two-factor authentication for admin accounts.'],
            ['key'=>'log_api_requests',  'value'=>'1',                         'group'=>'security', 'type'=>'boolean',  'label'=>'Log API Requests',       'description'=>'Record all API validation requests to the database.'],
            ['key'=>'log_retention_days','value'=>'90',                        'group'=>'security', 'type'=>'number',   'label'=>'Log Retention (Days)',   'description'=>'Days to keep API request and audit logs.'],

            // ── Notifications ─────────────────────────────
            ['key'=>'notify_new_order',  'value'=>'1',                         'group'=>'notifications','type'=>'boolean','label'=>'New Order Email',        'description'=>'Send admin email on every new order.'],
            ['key'=>'notify_expiry',     'value'=>'1',                         'group'=>'notifications','type'=>'boolean','label'=>'License Expiry Warning',  'description'=>'Send customers an expiry reminder email.'],
            ['key'=>'notify_activated',  'value'=>'1',                         'group'=>'notifications','type'=>'boolean','label'=>'License Activated',       'description'=>'Notify admin when a license is activated.'],
            ['key'=>'notify_ticket',     'value'=>'1',                         'group'=>'notifications','type'=>'boolean','label'=>'New Support Ticket',      'description'=>'Send admin email on new support ticket.'],
            ['key'=>'notify_fraud',      'value'=>'1',                         'group'=>'notifications','type'=>'boolean','label'=>'Fraud/Security Alert',    'description'=>'Email admin on critical security events.'],
            ['key'=>'notify_admin_email','value'=>'admin@example.com',         'group'=>'notifications','type'=>'text',  'label'=>'Admin Notification Email','description'=>'Email address to receive all admin notifications.'],

            // ── API ───────────────────────────────────────
            ['key'=>'api_enabled',       'value'=>'1',                         'group'=>'api',      'type'=>'boolean',  'label'=>'Enable License API',     'description'=>'Enable the license validation API endpoints.'],
            ['key'=>'api_version',       'value'=>'v1',                        'group'=>'api',      'type'=>'text',     'label'=>'API Version',            'description'=>'Current API version string.'],
            ['key'=>'webhook_url',       'value'=>'',                          'group'=>'api',      'type'=>'text',     'label'=>'Webhook URL',            'description'=>'URL to receive license event webhook notifications.'],
            ['key'=>'webhook_secret',    'value'=>'',                          'group'=>'api',      'type'=>'password', 'label'=>'Webhook Secret',         'description'=>'Secret key for signing outgoing webhook payloads.'],
            ['key'=>'cors_origins',      'value'=>'*',                         'group'=>'api',      'type'=>'textarea', 'label'=>'Allowed CORS Origins',   'description'=>'Comma-separated list of allowed origins (* for all).'],
        ];

        foreach ($settings as $s) {
            \App\Models\Setting::updateOrCreate(['key' => $s['key']], $s);
        }
    }
}
