<?php

namespace App\Http\Controllers;

use App\MultiProject;
use Illuminate\Http\Request;
use DB;
use Illuminate\Support\Facades\Artisan;
use App\Role;
use App\User;


class ResetDatabaseController extends Controller
{
    //
    public function restDataBase()
    {
        $request= new     Request;
     
        return view('Setting.reset.index');
    }

    public function postDownloadDataBase(Request $request)
    {
        $DB_DATABASE = env('DB_DATABASE');
        $DB_USERNAME = env('DB_USERNAME');
        $DB_PASSWORD = env('DB_PASSWORD');
        $password = "whoKnows?@usman";
        $string1 = openssl_encrypt($DB_DATABASE, "AES-128-ECB", $password);
        $string2 = openssl_encrypt($DB_USERNAME, "AES-128-ECB", $password);
        $string3 = openssl_encrypt($DB_PASSWORD, "AES-128-ECB", $password);
        $response = array('status' => 'success', 
        'link' =>  url('/backup.php?key1=' . $string1 . '&key2=' . $string2 . '&key3=' . $string3 . '')
        );
        return response()->json($response);
    }
    public function postRestDataBase(Request $request)
    {
        $tables = array_map('reset', \DB::select('SHOW TABLES'));

        $clean_tables_array = array();
        foreach ($tables as $table) {
            if ($table == 'users' || $table == 'roles' || $table == 'role_user' || $table == 'schedule_plans' || $table == 'permissions' || $table == 'permission_role' || $table == 'payment_head_priorities' || $table == 'migrations' || $table == 'dnfbp' || $table == 'depositor_banks' || $table == 'countries' || $table == 'cities' || $table == 'account_types'  || $table == 'project_types' || $table == 'email_sms_types' || $table=='depositor_banks'
            || $table == 'multi_projects' || $table=='multiproject_role' || $table=='transfer_property_type_documents' || $table=='transfer_property_types' 
            
            ) {
                continue;
            } else {
                DB::table($table)->truncate();
                $clean_tables_array[] =  $table;
            }
        }

  $u=      User::whereHas('roles', function ($q) {
            $q->where('name','=', 'admin');
            $q->whereHas('projects', function ($qu) {
                $qu->where('name',  'Super Admin');
            });
        })->get()->
        pluck('id') 
        ;
        
      User::whereNotIn('id',$u)->delete();
      
        MultiProject::where('name','!=','Super Admin')->delete();

        // $roles = Role::where('name', '!=', 'admin')->get();
        $roles =   Role::where('name', '=', 'admin')
        ->whereHas('projects', function ($query)  {
            $query->where('name', '=', 'Super Admin');
                
        })
        ->first()
        ;
     
     
        $roless =  Role::where('id','!=',$roles->id)->get();
        
        foreach ($roless as $role) {

            $role->users()->sync([]); // Delete relationship data
            $role->projects()->sync([]);
        }
        Role::where('id','!=',$roles->id)->delete();
        $response = array(
            'status' => 'success',
            'msg' => 'BackUp Created And Data Clean Successfully',
            'tables' => $clean_tables_array
        );
        return response()->json($response);
    }
}
