<?php

namespace Database\Seeders;

use App\Models\Order;
use App\Models\ProfitCenter;
use Carbon\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use JeroenZwart\CsvSeeder\CsvSeeder;

class OrderSeeder extends CsvSeeder
{
    public function __construct()
    {
        $reflect = new \ReflectionClass($this);
        $tableName = from_camel_case(str_replace('Seeder', '', $reflect->getShortName())).'s';
        $this->tablename = $tableName;
        $this->delimiter = ';';
        $this->file = base_path().'/database/csv/'.$this->tablename.'.csv';
        $this->truncate = false;
    }

    /**
     * Run the database seeds.
     */
    public function run(): void
    {

        // DB::disableQueryLog();
        \Schema::disableForeignKeyConstraints();
        DB::table($this->tablename)->delete();
        parent::run();
        \Schema::enableForeignKeyConstraints();
        Artisan::call('db:seed', [
            '--class' => 'OrderNumberSeeder',
        ]);
        $PC = ProfitCenter::where('hooreycaId', 'proba')->first();

        $Order = Order::find(2);
        $Order->created_by = auth()->user()->id;
        $Order->deliveryDate = Carbon::now()->format('Y-m-d');
        $Order->profit_center_id = $PC->id;
        $Order->deliveryAddress = $PC->fullAddress;
        $Order->save();
        $Order = Order::find(3);
        $Order->created_by = auth()->user()->id;
        $Order->deliveryDate = Carbon::now()->addDays(2)->format('Y-m-d');
        $Order->profit_center_id = $PC->id;
        $Order->deliveryAddress = $PC->fullAddress;
        // $Order->deliveryDate=Carbon::now()->next('Monday')->format('Y-m-d');
        $Order->save();
        $Order = Order::find(4);
        $Order->created_by = auth()->user()->id;
        $Order->deliveryDate = Carbon::now()->addDays(7)->next('Tuesday')->format('Y-m-d');
        $Order->profit_center_id = $PC->id;
        $Order->deliveryAddress = $PC->fullAddress;
        $Order->save();

    }
}
