<?php

namespace Database\Seeders;

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

class CompanySeeder 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\Company::all();

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

        // Insert records into your local database
        foreach ($externalRecords as $record) {
            \App\Models\Company::insert([
                'id' => $record->comp_id,
                'code' => $record->comp_code,
                'name' => $record->comp_name,
                'region' => $record->region,
                'address' => $record->comp_address,
                'city' => $record->comp_city,
                'phone_no' => $record->comp_contact,
                'fax_no' => $record->comp_fax,
                'tin' => $record->comp_tin,
                'email' => $record->comp_email,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);


        }
    }

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