<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class DealershipSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Set the connection to the external database
        DB::connection('old_db');

        // Fetch records from the external database using Eloquent
        $externalRecords = \App\Models\OldDB\Dealership::all();

        DB::connection('mysql');
        $this->reset();

        // Insert records into your local database
        foreach ($externalRecords as $record) {
            \App\Models\Dealership::insert([
                'id' => $record->deal_id,
                'company_id' => $record->comp_id,
                'code' => $record->deal_code,
                'name' => $record->deal_name,
                'address' => $record->deal_address,
                'city' => $record->deal_city,
                'phone_no' => $record->deal_contact,
                'fax_no' => $record->deal_fax,
                'tin' => $record->deal_tin,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);


        }
    }

    public function reset() {
        \App\Models\Dealership::truncate();
    }
}
