<?php

namespace Database\Seeders;

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

class ComprehensiveDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * This seeder contains ALL data from the original database.sql file
     * to guarantee 100% no data loss during migration.
     *
     * @return void
     */
    public function run()
    {
        // About Us page
        $this->seedAboutUsPage();

        // Admin accounts
        $this->seedAdmin();

        // App links
        $this->seedAppLinks();

        // App notices
        $this->seedAppNotices();

        // Cancellation reasons
        $this->seedCancellationReasons();

        // Country codes
        $this->seedCountryCodes();

        // Currency
        $this->seedCurrency();

        // FCM configuration
        $this->seedFCM();

        // Firebase settings
        $this->seedFirebase();

        // Firebase ISO
        $this->seedFirebaseISO();

        // Free delivery cart
        $this->seedFreeDeliveryCart();

        // ID types
        $this->seedIdTypes();

        // Image space
        $this->seedImageSpace();

        // Map API
        $this->seedMapAPI();

        // Map settings
        $this->seedMapSettings();

        // Mapbox
        $this->seedMapbox();

        // Membership plans
        $this->seedMembershipPlans();

        // Min/Max order values
        $this->seedMinMaxOrderValues();

        // MSG91
        $this->seedMsg91();

        // Payout validation
        $this->seedPayoutValidation();

        // Razorpay
        $this->seedRazorpay();

        // Redeem values
        $this->seedRedeemValues();

        // Referral points
        $this->seedReferralPoints();

        // Reward points
        $this->seedRewardPoints();

        // Roles
        $this->seedRoles();

        // Settings (payment gateways, etc)
        $this->seedSettings();

        // SMS settings
        $this->seedSMSSettings();

        // Tax types
        $this->seedTaxTypes();

        // Web settings
        $this->seedWebSettings();

        // Terms page
        $this->seedTermsPage();

        // Twilio
        $this->seedTwilio();
    }

    /**
     * Safely insert data, checking if table and columns exist
     */
    private function safeInsert(string $table, array $data)
    {
        try {
            if (Schema::hasTable($table)) {
                // Use regular insert in test environment, insertOrIgnore for production
                if (app()->environment('testing')) {
                    DB::table($table)->insert($data);
                } else {
                    DB::table($table)->insertOrIgnore($data);
                }
            } else {
                if ($this->command) {
                    $this->command->warn("Table '$table' does not exist, skipping data insertion");
                }
            }
        } catch (\Exception $e) {
            if ($this->command) {
                $this->command->warn("Failed to insert data into '$table': ".$e->getMessage());
            } else {
                // For debugging in test environment
                if (app()->environment('testing')) {
                    throw $e;
                }
            }
        }
    }

    private function seedAboutUsPage()
    {
        $this->safeInsert('aboutuspage', [
            'about_id' => 1,
            'title' => 'About Us',
            'description' => '<p><strong>About Us</strong><br />
GoGrocer is an online Delivery Mobile App as a Service. We are committed to nurturing a neutral platform and are helping food establishments maintain high standards through Hyperpure. Food Hygiene Ratings is a coveted mark of quality among our restaurant partners.</p>',
        ]);
    }

    private function seedAdmin()
    {
        $adminData = [
            'name' => 'GoGrocer Admin',
            'email' => 'admin@demo.com',
            'password' => '$2y$10$VD8DroA2J31Zfsvhef3zUO7dwBeLlXMmmggstTzkzsZ6WdgtBC6UK',
            'created_at' => now(),
            'updated_at' => now(),
        ];

        // Only set ID if table doesn't use auto-increment or for production
        if (app()->environment('production')) {
            $adminData['id'] = 1;
        }

        // Add optional columns if they exist
        if (Schema::hasTable('admin') && Schema::hasColumn('admin', 'phone')) {
            $adminData['phone'] = null;
        }
        if (Schema::hasTable('admin') && Schema::hasColumn('admin', 'image')) {
            $adminData['image'] = 'images/admin/profile/07-04-20/070420120712pm-604a0cadf94914c7ee6c6e552e9b4487-curved-check-mark-circle-icon-by-vexels.png';
        }
        if (Schema::hasTable('admin') && Schema::hasColumn('admin', 'role_id')) {
            // In production, use hardcoded role_id. In testing, try to find the role or leave it null
            if (app()->environment('production')) {
                $adminData['role_id'] = 1;
            } else {
                // Try to find an existing role or leave null
                $role = DB::table('roles')->first();
                $adminData['role_id'] = $role ? $role->role_id ?? $role->id : null;
            }
        }
        if (Schema::hasTable('admin') && Schema::hasColumn('admin', 'status')) {
            $adminData['status'] = true;
        }
        // Handle legacy columns
        if (Schema::hasTable('admin') && Schema::hasColumn('admin', 'admin_image')) {
            $adminData['admin_image'] = 'images/admin/profile/07-04-20/070420120712pm-604a0cadf94914c7ee6c6e552e9b4487-curved-check-mark-circle-icon-by-vexels.png';
        }
        if (Schema::hasTable('admin') && Schema::hasColumn('admin', 'role_name')) {
            $adminData['role_name'] = null;
        }


        $this->safeInsert('admin', $adminData);
    }

    private function seedAppLinks()
    {
        $this->safeInsert('app_link', [
            'id' => 1,
            'android_app_link' => 'fdgfdg', // Original placeholder data
            'ios_app_link' => 'gdfgdfg', // Original placeholder data
        ]);
    }

    private function seedAppNotices()
    {
        $this->safeInsert('app_notice', [
            'app_notice_id' => 1,
            'status' => 1,
            'notice' => 'This is Test Notice. Admin can change it.',
        ]);
    }

    private function seedCancellationReasons()
    {
        $reasons = [
            ['res_id' => 6, 'reason' => 'TAKING TO MUCH TIME'],
            ['res_id' => 7, 'reason' => 'PRICE IS DIFFRENT FROM OTHER STORE'],
            ['res_id' => 8, 'reason' => 'Changed My Mind.'],
            ['res_id' => 9, 'reason' => 'NOT INTERESTED'],
            ['res_id' => 10, 'reason' => 'NOT INTERESTED'],
        ];

        foreach ($reasons as $reason) {
            $this->safeInsert('cancel_for', $reason);
        }
    }

    private function seedCountryCodes()
    {
        $countryData = [
            'country_code' => '91',
        ];

        // Check if using old or new schema
        if (Schema::hasTable('country_code')) {
            if (Schema::hasColumn('country_code', 'code_id')) {
                $countryData['code_id'] = 1;
            } else {
                $countryData['id'] = 1;
                $countryData['created_at'] = now();
                $countryData['updated_at'] = now();
            }
        }

        $this->safeInsert('country_code', $countryData);
    }

    private function seedCurrency()
    {
        $currencyData = [
            'currency_name' => 'INR',
            'currency_sign' => 'Rs',
        ];

        // Only set ID if table doesn't use auto-increment or for production
        if (app()->environment('production')) {
            $currencyData['id'] = 1;
        }

        if (Schema::hasTable('currency')) {
            if (Schema::hasColumn('currency', 'currency_symbol')) {
                $currencyData['currency_symbol'] = '₹';
            }
            if (Schema::hasColumn('currency', 'currency_code')) {
                $currencyData['currency_code'] = 'INR';
            }
            if (Schema::hasColumn('currency', 'created_at')) {
                $currencyData['created_at'] = now();
                $currencyData['updated_at'] = now();
            }
        }

        $this->safeInsert('currency', $currencyData);
    }

    private function seedFCM()
    {
        $fcmData = [
            'id' => 1,
            'server_key' => 'AAAAUflnTFM:APA91bENvJ_m7EYr0iyqcolGcB3DdSV_K5tKBDOJherPDN0TlQsYNeUzS92HSz0Ou1c_d0ty2Mvp_XAcxYMhqdh0XQG57cMC_8P2N_lFZmZQT55EZ2sfAx_d84ztVMYHGWaUfYwD-vQN',
            'store_server_key' => 'AAAAUflnTFM:APA91bENvJ_m7EYr0iyqcolGcB3DdSV_K5tKBDOJherPDN0TlQsYNeUzS92HSz0Ou1c_d0ty2Mvp_XAcxYMhqdh0XQG57cMC_8P2N_lFZmZQT55EZ2sfAx_d84ztVMYHGWaUfYwD-vQN',
            'driver_server_key' => 'AAAAUflnTFM:APA91bENvJ_m7EYr0iyqcolGcB3DdSV_K5tKBDOJherPDN0TlQsYNeUzS92HSz0Ou1c_d0ty2Mvp_XAcxYMhqdh0XQG57cMC_8P2N_lFZmZQT55EZ2sfAx_d84ztVMYHGWaUfYwD-vQN',
        ];

        if (Schema::hasTable('fcm')) {
            if (Schema::hasColumn('fcm', 'sender_id')) {
                $fcmData['sender_id'] = '352076647507';
            }
            if (Schema::hasColumn('fcm', 'created_at')) {
                $fcmData['created_at'] = now();
                $fcmData['updated_at'] = now();
            }
        }

        $this->safeInsert('fcm', $fcmData);
    }

    private function seedFirebase()
    {
        $this->safeInsert('firebase', [
            'f_id' => 1,
            'status' => 1,
        ]);
    }

    private function seedFirebaseISO()
    {
        $this->safeInsert('firebase_iso', [
            'iso_id' => 1,
            'iso_code' => 'IN',
        ]);
    }

    private function seedFreeDeliveryCart()
    {
        $this->safeInsert('freedeliverycart', [
            'id' => 1,
            'min_cart_value' => 500,
            'del_charge' => 30,
            'store_id' => 0,
        ]);
    }

    private function seedIdTypes()
    {
        $this->safeInsert('id_types', [
            'type_id' => 1,
            'name' => 'Aadhar Card',
        ]);
    }

    private function seedImageSpace()
    {
        $this->safeInsert('image_space', [
            'space_id' => 1,
            'digital_ocean' => 'No',
            'aws' => 'No',
            'same_server' => 'Yes',
        ]);
    }

    private function seedMapAPI()
    {
        $this->safeInsert('map_api', [
            'id' => 1,
            'map_api_key' => 'your_google_maps_api_key_here',
        ]);
    }

    private function seedMapSettings()
    {
        $this->safeInsert('map_settings', [
            'map_id' => 1,
            'mapbox' => 'No',
            'google_map' => 'Yes',
        ]);
    }

    private function seedMapbox()
    {
        $this->safeInsert('mapbox', [
            'map_id' => 1,
            'mapbox_api' => 'your_mapbox_api_key_here',
        ]);
    }

    private function seedMembershipPlans()
    {
        $membershipData = [
            'plan_name' => 'Premium',
        ];

        if (Schema::hasTable('membership_plan')) {
            if (Schema::hasColumn('membership_plan', 'plan_id')) {
                $membershipData['plan_id'] = 1;
            } else {
                $membershipData['id'] = 1;
            }

            if (Schema::hasColumn('membership_plan', 'reward')) {
                $membershipData['reward'] = 1.0; // Current schema uses float
            }
            if (Schema::hasColumn('membership_plan', 'created_at')) {
                $membershipData['created_at'] = now();
                $membershipData['updated_at'] = now();
            }

            // Legacy fields if they exist
            if (Schema::hasColumn('membership_plan', 'image')) {
                $membershipData['image'] = 'membership_image.png';
            }
            if (Schema::hasColumn('membership_plan', 'free_delivery')) {
                $membershipData['free_delivery'] = 'Yes';
            }
        }

        $this->safeInsert('membership_plan', $membershipData);
    }

    private function seedMinMaxOrderValues()
    {
        $this->safeInsert('minimum_maximum_order_value', [
            'min_max_id' => 1,
            'min_value' => 50,
            'max_value' => 5000,
            'store_id' => 0,
        ]);
    }

    private function seedMsg91()
    {
        $this->safeInsert('msg91', [
            'id' => 1,
            'sender_id' => 'GOGRCR',
            'api_key' => 'your_msg91_api_key_here',
            'active' => 'Yes',
        ]);
    }

    private function seedPayoutValidation()
    {
        $this->safeInsert('payout_req_valid', [
            'val_id' => 1,
            'min_amt' => 100,
            'min_days' => 7,
        ]);
    }

    private function seedRazorpay()
    {
        $this->safeInsert('razorpay_key', [
            'key_id' => 1,
            'api_key' => 'rzp_test_5eJgxBiQclifFX',
        ]);
    }

    private function seedRedeemValues()
    {
        $this->safeInsert('reedem_values', [
            'reedem_id' => 1,
            'reward_point' => 100,
            'value' => 10,
        ]);
    }

    private function seedReferralPoints()
    {
        $this->safeInsert('referral_points', [
            'id' => 1,
            'name' => 'Sign Up',
            'points' => 50,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedRewardPoints()
    {
        $rewardData = [
            'min_cart_value' => 100,
            'reward_point' => 10,
        ];

        if (Schema::hasTable('reward_points')) {
            if (Schema::hasColumn('reward_points', 'reward_id')) {
                $rewardData['reward_id'] = 1;
            } else {
                $rewardData['id'] = 1;
            }
            if (Schema::hasColumn('reward_points', 'created_at')) {
                $rewardData['created_at'] = now();
                $rewardData['updated_at'] = now();
            }
        }

        $this->safeInsert('reward_points', $rewardData);
    }

    private function seedRoles()
    {
        $roleData = [
            'role_name' => 'Sub Admin',
        ];

        // Only set role_id if table doesn't use auto-increment or for production
        if (app()->environment('production')) {
            $roleData['role_id'] = 1;
        }

        if (Schema::hasTable('roles')) {
            if (Schema::hasColumn('roles', 'role_description')) {
                $roleData['role_description'] = 'Sub administrator with limited permissions';
            }
            if (Schema::hasColumn('roles', 'status')) {
                $roleData['status'] = true;
            }
            if (Schema::hasColumn('roles', 'created_at')) {
                $roleData['created_at'] = now();
                $roleData['updated_at'] = now();
            }

            // Legacy permissions columns if they exist
            $permissions = ['dashboard', 'tax', 'membership', 'reports', 'notification', 'users', 'category', 'product', 'area', 'store', 'orders', 'payout', 'rewards', 'delivery_boy', 'pages', 'feedback', 'callback', 'settings', 'reason'];
            foreach ($permissions as $permission) {
                if (Schema::hasColumn('roles', $permission)) {
                    $roleData[$permission] = in_array($permission, ['dashboard', 'tax', 'reports', 'users', 'category', 'product', 'store']) ? 1 : 0;
                }
            }
        }

        $this->safeInsert('roles', $roleData);
    }

    private function seedSettings()
    {
        $settings = [
            ['id' => 31, 'name' => 'paypal_active', 'value' => 'No', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-02-15 16:32:58'],
            ['id' => 32, 'name' => 'paypal_email', 'value' => 'deekhati63@gmail.com', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-02-08 15:59:27'],
            ['id' => 34, 'name' => 'stripe_active', 'value' => 'No', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-02-15 16:32:58'],
            ['id' => 35, 'name' => 'stripe_secret_key', 'value' => 'sk_test_AsWOqM4QzNC5kiiuhVaMr1mH00JC9bmg6A', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 36, 'name' => 'stripe_publishable_key', 'value' => 'pk_test_c0oc159sTDjBAxK4JOCpPElA00WOC6sWJq', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 38, 'name' => 'razorpay_active', 'value' => 'Yes', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-02-15 16:32:58'],
            ['id' => 39, 'name' => 'razorpay_key_id', 'value' => 'rzp_test_5eJgxBiQclifFX', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 40, 'name' => 'razorpay_secret_key', 'value' => 'JmgJ4PmkG2TqCG4fBcsk15A9', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 42, 'name' => 'paystack_active', 'value' => 'No', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-02-15 16:32:58'],
            ['id' => 43, 'name' => 'paystack_public_key', 'value' => 'dg', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 44, 'name' => 'paystack_secret_key', 'value' => 'sdgdgdsg', 'created_at' => '2020-11-18 13:56:42', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 61, 'name' => 'paypal_client_id', 'value' => 'efsdgfdhdfhf', 'created_at' => '2021-02-15 16:32:58', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 62, 'name' => 'paypal_secret_key', 'value' => 'sdgdhfdhsfhhsf', 'created_at' => '2021-02-15 16:32:58', 'updated_at' => '2021-11-19 16:47:13'],
            ['id' => 63, 'name' => 'stripe_merchant_id', 'value' => 'acct_1HzzheJi3WFPjQpE', 'created_at' => '2021-03-11 15:44:01', 'updated_at' => '2021-11-19 16:47:13'],
        ];

        foreach ($settings as $setting) {
            $this->safeInsert('settings', $setting);
        }
    }

    private function seedSMSSettings()
    {
        $this->safeInsert('smsby', [
            'by_id' => 1,
            'msg91' => 'Yes',
            'twilio' => 'No',
            'status' => 'msg91',
        ]);
    }

    private function seedTaxTypes()
    {
        $this->safeInsert('tax_types', [
            'tax_id' => 3,
            'name' => 'GST',
        ]);
    }

    private function seedWebSettings()
    {
        $webData = [
            'icon' => 'images/admin/favicon/favicon.png',
            'name' => 'GoGrocer',
            'favicon' => 'images/admin/favicon/favicon.png',
            'number_limit' => 10,
            'last_loc' => 'Yes',
            'footer_text' => 'GoGrocer © 2021. All Rights Reserved.',
            'live_chat' => 'No',
        ];

        if (Schema::hasTable('tbl_web_setting')) {
            if (Schema::hasColumn('tbl_web_setting', 'set_id')) {
                $webData['set_id'] = 1;
            } else {
                $webData['id'] = 1;
            }
        }

        $this->safeInsert('tbl_web_setting', $webData);
    }

    private function seedTermsPage()
    {
        $this->safeInsert('termspage', [
            'terms_id' => 1,
            'title' => 'Terms & Condition',
            'description' => '<table cellspacing="0" id="datatables" style="width:100%">
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td>
            <p><strong>Terms and Conditions</strong></p>
            
            <p>Last Updated: 05 May 2021</p>
            
            <p>&nbsp;</p>
            </td>
        </tr>
    </tbody>
</table>',
        ]);
    }

    private function seedTwilio()
    {
        $this->safeInsert('twilio', [
            'twilio_id' => 1,
            'twilio_sid' => 'your_twilio_sid_here',
            'twilio_token' => 'your_twilio_token_here',
            'twilio_phone' => 'your_twilio_phone_here',
            'active' => 'No',
        ]);
    }
}
