<?php

namespace Database\Seeders;

use App\Models\SystemSetting;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

/**
 * SystemSettingsSeeder
 *
 * Seeds all default system configuration key-value pairs consumed by the
 * SSHIA platform. These settings drive dynamic behaviour across every module —
 * from organisation branding and OTP authentication to membership fees,
 * training certificates, and site-wide operational flags.
 *
 * Organisation details reflect the official registered information of the
 * South Sudan Health Informatics Association (SSHIA).
 *
 * Uses INSERT IGNORE (insertOrIgnore) so existing production values are
 * never overwritten on re-seed — safe to run on a live database.
 */
class SystemSettingsSeeder extends Seeder
{
    public function run(): void
    {
        $settings = [
            // ----------------------------------------------------------------
            // Organisation Identity
            // ----------------------------------------------------------------
            [
                'key'         => 'org_name',
                'value'       => 'South Sudan Health Informatics Association',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Full legal name of the organisation displayed across the platform.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_short_name',
                'value'       => 'SSHIA',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Abbreviated organisation name used in certificates, SMS, and compact UI elements.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_email',
                'value'       => 'info@sshia.org',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Primary contact email address for the organisation.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_phone',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Primary contact phone number for the organisation.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_website',
                'value'       => 'https://www.sshia.org',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Official website URL of the organisation.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_address',
                'value'       => 'School of Public Health, University of Juba, P.O BOX 82, Juba, South Sudan',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Full postal and physical address of the organisation.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_city',
                'value'       => 'Juba',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'City where the organisation is headquartered.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_country',
                'value'       => 'South Sudan',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Country where the organisation is registered.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_logo',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Path to the organisation logo image file (relative to storage/app/public).',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_favicon',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Path to the site favicon image file.',
                'is_public'   => true,
            ],
            [
                'key'         => 'org_year_founded',
                'value'       => '2018',
                'type'        => 'string',
                'group'       => 'organization',
                'description' => 'Year the organisation was formally established.',
                'is_public'   => true,
            ],

            // ----------------------------------------------------------------
            // Social Media Links
            // ----------------------------------------------------------------
            [
                'key'         => 'social_facebook',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'social',
                'description' => 'Full URL to the organisation\'s official Facebook page.',
                'is_public'   => true,
            ],
            [
                'key'         => 'social_twitter',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'social',
                'description' => 'Full URL to the organisation\'s official Twitter/X profile.',
                'is_public'   => true,
            ],
            [
                'key'         => 'social_linkedin',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'social',
                'description' => 'Full URL to the organisation\'s official LinkedIn page.',
                'is_public'   => true,
            ],
            [
                'key'         => 'social_youtube',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'social',
                'description' => 'Full URL to the organisation\'s official YouTube channel.',
                'is_public'   => true,
            ],

            // ----------------------------------------------------------------
            // Authentication & OTP Settings
            // ----------------------------------------------------------------
            [
                'key'         => 'otp_enabled',
                'value'       => 'false',
                'type'        => 'boolean',
                'group'       => 'auth',
                'description' => 'Enable or disable OTP (One-Time Password) verification for user login.',
                'is_public'   => false,
            ],
            [
                'key'         => 'otp_expiry_minutes',
                'value'       => '10',
                'type'        => 'integer',
                'group'       => 'auth',
                'description' => 'Number of minutes before an OTP code expires.',
                'is_public'   => false,
            ],
            [
                'key'         => 'otp_max_attempts',
                'value'       => '3',
                'type'        => 'integer',
                'group'       => 'auth',
                'description' => 'Maximum number of failed OTP attempts before the code is invalidated.',
                'is_public'   => false,
            ],
            [
                'key'         => 'two_factor_enabled',
                'value'       => 'false',
                'type'        => 'boolean',
                'group'       => 'auth',
                'description' => 'Enable or disable two-factor authentication (2FA) for all users.',
                'is_public'   => false,
            ],
            [
                'key'         => 'login_max_attempts',
                'value'       => '5',
                'type'        => 'integer',
                'group'       => 'auth',
                'description' => 'Maximum number of failed login attempts before account lockout.',
                'is_public'   => false,
            ],
            [
                'key'         => 'login_lockout_minutes',
                'value'       => '15',
                'type'        => 'integer',
                'group'       => 'auth',
                'description' => 'Duration in minutes for which an account is locked after exceeding max login attempts.',
                'is_public'   => false,
            ],

            // ----------------------------------------------------------------
            // Membership Settings
            // ----------------------------------------------------------------
            [
                'key'         => 'membership_auto_approve',
                'value'       => 'false',
                'type'        => 'boolean',
                'group'       => 'membership',
                'description' => 'Automatically approve membership applications without manual admin review.',
                'is_public'   => false,
            ],
            [
                'key'         => 'membership_renewal_reminder_days',
                'value'       => '30',
                'type'        => 'integer',
                'group'       => 'membership',
                'description' => 'Number of days before membership expiry to send renewal reminder notifications.',
                'is_public'   => false,
            ],
            [
                'key'         => 'membership_grace_period_days',
                'value'       => '30',
                'type'        => 'integer',
                'group'       => 'membership',
                'description' => 'Number of days after membership expiry during which the member retains access.',
                'is_public'   => false,
            ],

            // ----------------------------------------------------------------
            // Training Settings
            // ----------------------------------------------------------------
            [
                'key'         => 'training_certificate_prefix',
                'value'       => 'SSHIA-CERT',
                'type'        => 'string',
                'group'       => 'training',
                'description' => 'Prefix used when generating unique training certificate numbers (e.g. SSHIA-CERT-2026-00001).',
                'is_public'   => false,
            ],
            [
                'key'         => 'training_certificate_attendance_threshold',
                'value'       => '80',
                'type'        => 'integer',
                'group'       => 'training',
                'description' => 'Minimum attendance percentage (0–100) required for a trainee to qualify for a training certificate.',
                'is_public'   => false,
            ],

            // ----------------------------------------------------------------
            // Mentorship Settings
            // ----------------------------------------------------------------
            [
                'key'         => 'mentorship_certificate_prefix',
                'value'       => 'SSHIA-MENT',
                'type'        => 'string',
                'group'       => 'mentorship',
                'description' => 'Prefix used when generating unique mentorship certificate numbers (e.g. SSHIA-MENT-2026-00001).',
                'is_public'   => false,
            ],

            // ----------------------------------------------------------------
            // Site-Wide Settings
            // ----------------------------------------------------------------
            [
                'key'         => 'site_maintenance_mode',
                'value'       => 'false',
                'type'        => 'boolean',
                'group'       => 'site',
                'description' => 'Put the site into maintenance mode. Authenticated Super Managers can still access the system.',
                'is_public'   => false,
            ],
            [
                'key'         => 'site_registration_open',
                'value'       => 'true',
                'type'        => 'boolean',
                'group'       => 'site',
                'description' => 'Allow new user registrations. Set to false to close public registration.',
                'is_public'   => false,
            ],
            [
                'key'         => 'site_tagline',
                'value'       => 'Advancing Health Informatics in South Sudan',
                'type'        => 'string',
                'group'       => 'site',
                'description' => 'Short tagline displayed in the site header and meta description.',
                'is_public'   => true,
            ],
            [
                'key'         => 'site_meta_description',
                'value'       => 'The South Sudan Health Informatics Association (SSHIA) is the professional body for health informatics practitioners in South Sudan, dedicated to strengthening health information systems and improving health outcomes.',
                'type'        => 'string',
                'group'       => 'site',
                'description' => 'Default meta description for SEO purposes, used on pages without a custom meta description.',
                'is_public'   => true,
            ],
            [
                'key'         => 'site_google_analytics_id',
                'value'       => '',
                'type'        => 'string',
                'group'       => 'site',
                'description' => 'Google Analytics Measurement ID (e.g. G-XXXXXXXXXX) for site traffic tracking.',
                'is_public'   => false,
            ],
        ];

        // Use insertOrIgnore to preserve any values already set in production
        foreach ($settings as $setting) {
            DB::table('system_settings')->insertOrIgnore([
                'key'         => $setting['key'],
                'value'       => $setting['value'],
                'type'        => $setting['type'],
                'group'       => $setting['group'],
                'description' => $setting['description'],
                'is_public'   => $setting['is_public'] ? 1 : 0,
                'created_at'  => now(),
                'updated_at'  => now(),
            ]);
        }

        $count = count($settings);
        $this->command->info("SystemSettingsSeeder: {$count} system settings seeded (existing values preserved).");
    }
}
