<?php

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

class StanbicTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        /**
         * Stanic Bank Users:
         *
         * Normal User: stanbicgh
         * Reseller User: stanbic admin (password:
         */

        $resellerRole = DB::table('roles')
            ->where('name', 'reseller')
            ->first();

        $existingReseller = DB::table('users')->where([
            ['role_id', '=', $resellerRole->id],
            ['username', '=', 'stanbic admin'],
        ])->first();

        //check and create reseller user
        if(empty($existingReseller)) {
            $existingResellerId = DB::table('users')->insertGetId([
                'organisation' => 'Stanbic Bank',
                'first_name' => 'Stanbic',
                'last_name' => 'Bank',
                'full_name' => 'Stanbic Bank',
                'phone_number' => '233501323377',
                'username' => 'stanbic admin',
                'email' => 'stanbic@npontu.com',
                'password' => bcrypt('passadminstan'),
                'role_id' => $resellerRole->id,
                'created_by' => 'npontu',
                'is_locked' => false,
                'must_change_password' => false,
                'status' => 'active',
                'pass' => 'passadminstan',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
            if(!empty($existingResellerId)) {
                $existingReseller = DB::table('users')->find($existingResellerId);

                //create credit
                DB::table('credits')->insert([
                    'username' => $existingReseller->username,
                    'price' => '0.02',
                    'email_price' => '0.00332',
                    'credit_alloted' => '1',
                    'old_bal' => '0',
                    'new_bal' => '1',
                    'credit_used' => '0',
                    'credited' => '0',
                    'user_id' => $existingReseller->id,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);
            } else {
                throw new Exception('Cannot create Stanbic Reseller');
            }
        }

        //check and create normal user
        $resellerRole = DB::table('roles')
            ->where('name', 'user')
            ->first();

        $existingUser = DB::table('users')->where([
            ['role_id', '=', $resellerRole->id],
            ['username', '=', 'stanbicgh'],
        ])->first();

        if(empty($existingUser)) {
            $existingUserId = DB::table('users')->insertGetId([
                'organisation' => 'Stanbic Bank',
                'first_name' => 'Plange',
                'last_name' => 'George',
                'full_name' => 'George Plange',
                'phone_number' => '233501323377',
                'username' => 'stanbicgh',
                'email' => 'Plangeg@stanbic.com.gh',
                'password' => bcrypt('passghstan'),
                'role_id' => $resellerRole->id,
                'created_by' => 'stanbic admin',
                'is_locked' => false,
                'must_change_password' => false,
                'status' => 'active',
                'pass' => 'passghstan',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
            if(!empty($existingUserId)) {
                $existingUser = DB::table('users')->find($existingUserId);

                //create credit
                DB::table('credits')->insert([
                    'username' => $existingUser->username,
                    'price' => '0.03',
                    'email_price' => '0.00332',
                    'credit_alloted' => '0.15',
                    'old_bal' => '0',
                    'new_bal' => '0.15',
                    'credit_used' => '0',
                    'credited' => '0',
                    'user_id' => $existingUser->id,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);
            } else {
                throw new Exception('Cannot create Stanbic User');
            }
        } else {
            //update reseller
            DB::table('users')->where('id', $existingUser->id)->update([
                'created_by' => 'stanbic admin',
            ]);
        }

        //seed permission
        $permissions = [
            //dashboard
            'view_sms_dashboard|app' => 'View SMS Dashboard',
            'view_email_dashboard|app.email' => 'View Email Dashboard',
            //SMS
            'send_sms_single|send_single_sms' => 'Send Single SMS',
            'send_sms_bulk|send_bulk_sms' => 'Send Bulk SMS',
            'send_sms_group|send_group_sms' => 'Send Group SMS',
            'send_sms_bulk_perso|send_personalised_bulk_sms' => 'Send Personalised Bulk SMS',
            'send_sms_group_perso|send_personalised_group_sms' => 'Send Personalised Group SMS',
            //SMS
            'send_voice_single|send_single_voice' => 'Send Single Voice',
            //Email
            'send_email_single|send_single_email' => 'Send Single Email',
            'send_email_bulk|send_bulk_email' => 'Send Bulk Email',
            'send_email_group|send_group_email' => 'Send Group Email',
            'send_email_bulk_perso|send_personalised_bulk_email' => 'Send Personalised Bulk Email',
            'send_email_group_perso|send_personalised_group_email' => 'Send Personalised Group Email',

            //Contact
            'manage_contacts|addressbook.groups' => 'Manage Address Book',

            //Report (Stambic bank Specific)
            'view_report_otp|report_otp' => 'View OTP Report',
            'view_report_transactional|report_transactional' => 'View Transactional Report',
            'view_report_bulksms|report_bulksms' => 'View Bulk SMS Report',
            //Report (existing)
            'view_report_24h_sms|report_get_sms_log_24h' => 'View 24h SMS Report',
            'view_report_24h_email|report_get_email_log_24h' => 'View 24h Email Report',
            'view_report_specific_sms|report_get_specify_period' => 'View Specific Report for SMS',
            'view_report_specific_email|report_get_specify_period_email' => 'View Specific Report for Email',
            'view_report_reporting|report_get_reporting' => 'View Reporting',
            'view_report_comparison|report_get_comparison' => 'View Comparison',
            'view_report_2way_msg|report_get_2way' => 'View 2 Way msg Response',
            'view_report_credit_history|credit_history' => 'View Credit History',
            'view_report_invoice|report_get_invoice' => 'View Invoice Report',
            //campaign
            'view_campaign_bulk_sms|campaigns.dashboard' => 'View Campaign on Bulk SMS',
            'view_campaign_bulk_email|campaigns.email.dashboard' => 'View Campaign on Bulk Email',
            'view_campaign_api_sms|campaigns.api' => 'View Campaign on SMS Api',
            'view_campaign_api_email|campaigns.email.api' => 'View Campaign on Email Api',
            'edit_profile|profile' => 'Update Profile'
        ];

        foreach ($permissions as $key => $value) {
            $p_name = explode('|', $key)[0];
            $p_route = explode('|', $key)[1];
            $existingPermission = DB::table('permissions')->where('name', $p_name)->first();
            if(empty($existingPermission)) {
                DB::table('permissions')->insert([
                    'name' => $p_name,
                    'description' => $value,
                    'route_name' => $p_route,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);
            }
        }

        //seed admin group
        $existingGroup = DB::table('groups')->where('name', 'admin')->first();
        if(empty($existingGroup)) {
            $existingGroupId = DB::table('groups')->insertGetId([
               'name' => 'admin',
               'description' => 'Stanbic User',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
            $existingGroup = DB::table('groups')->find($existingGroupId);
        }

        //add all permissions to group
        foreach (DB::table('permissions')->get() as $perm) {
            $existingGP = DB::table('group_permission')
                ->where('group_id', $existingGroup->id)
                ->where('permission_id', $perm->id)
                ->first();
            if(empty($existingGP)) {
                DB::table('group_permission')->insert([
                    'group_id' => $existingGroup->id,
                    'permission_id' => $perm->id,
                ]);
            }
        }

        //add user to stambic users
        $existingStanbicUser = DB::table('stanbic_users')->where('user_id', $existingUser->id)->first();
        if(empty($existingStanbicUser)) {
            DB::table('stanbic_users')->insert([
                'user_id'=>$existingUser->id,
                'username'=>$existingUser->username,
                'password'=>$existingUser->pass,
                'group_id'=>$existingGroup->id,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
        }
    }
}
