<?php

namespace App\Console\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CloneDatabase extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:clone';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate Laravel migrations from an existing database';


    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $tables = DB::connection('mysql7')->select("SHOW TABLES");

        $namedatabase = DB::connection('mysql7')->getDatabaseName();

        if (empty($tables)) {
            $this->error("No tables found in database");
            return;
        }

        $boolreject = false;
        $list_of_rejected =  ($boolreject) ? $this->rejected_table() : [];
        // Get table names
        $tableNames = array_map(fn($table) => is_array($table) ? array_values($table) : $table, $tables);
        $tableString = collect($tables)->pluck('Tables_in_' .$namedatabase)->reject(fn($table) => in_array($table, $list_of_rejected))->implode(',');
        // Run migration generator
        Artisan::call("migrate:generate --path=database/migrations --tables=" .$tableString);
        // Artisan::call("migrate:status");
        
        $this->info("Migrations generated successfully for database");
    }

    protected function rejected_table() : Array{
        return  [
            'alert','auditbarcode','auditdaftar',
            'auditrekodsemakan','audtrail','bakipelanggan',
            'bandar','batchlel_xtra','bayaranpenebusan','belian',
            'brand','checker','configkaunter','gadaisimulasi','gadaitemp',
            'gadaixtra','hapusgadaionline','hargaemasbeli',
            'hargaemasjual','imbangan','inoutmarxtra',
            'jemas00', 'jemasbeli','jemasxtra',
            'jenisakaun','jualan','jualemas','jumlahtebus',
            'karat','karatbeli','karatjual','karatxtra',
            'ku_nm', 'ku_nmbaru', 'ku_nmxtra', 'kumpulan',
            'kurangxtra', 'lapringkasan', 'laptebus', 'lapterkumpul',
            'lapterkumpulharian','lelkarat','lelmarhun_xtra', 'marhunbeli',
            'marhunjual', 'marhunsimulasi', 'marhunxtra', 'model',
            'outmarhun', 'outmarhunxtra', 'palsu', 'pemeriksaan',
            'pengesahangadai', ' peratusan', 'percentonline',
            'polisxtra','product','ramalan', 'rekodtangguh', 'sahabat',
            'simpanan', 'simulasi_batchlel', 'simulasi_batchlelxtra',
            'simulasi_lelmarhun', 'simulasi_lelong', 'simulasi_lelongxtra',
            'tagpelanggan', 'tangguhlelong', 'taraf',
            'tebus_xtra', 'tebuslelong', 'temp_ic', 'tempimbang',
            'tempnosiri', 'tempnosirixtra', 'temppwd', 'temptarikhaging',
            'tunaigadteb', 'tuntutbakixtra', 'ubs_code', 'ugama',
            'updateserver', 'zparameter', 'zparameterxtra','ZPARAMETER1'];
    }
}
