<?php

use Illuminate\Database\Seeder;

use App\Trouble;
use App\TroubleOld;
use App\TroublePrice;
use App\TroublePriceOld;
use App\Helpers\Helper;

class CloneTroubleData extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        TroublePrice::truncate();

        $list = TroubleOld::with('detail')->get();
        foreach ($list as $item) {
            $chkTrouble = Trouble::where('name', $item->name)->first();
            if (!isset($chkTrouble)) {
                $chkTrouble = Trouble::create([
                    'name'         => $item->name,
                    'description'  => $item->description,
                    'is_activated' => 1,
                    'is_deleted'   => 0,
                    'created_at'   => date('Y-m-d H:i:s'),
                    'updated_at'   => date('Y-m-d H:i:s'),
                ]);

                print "Created Trouble: " . $chkTrouble->name . " -- " . $chkTrouble->id . "\r\n";
            }

            foreach ($item->detail as $d) {
                $brandM = \App\BrandModel::where('name', $d->model_detail->name)->first();
                if (!isset($brandM)) {
                    $brandM = \App\BrandModel::create([
                        'brand_id'     => $d->model_detail->id,
                        'name'         => $d->model_detail->name,
                        'description'  => $d->model_detail->description,
                        'is_activated' => 1,
                        'is_deleted'   => 0,
                        'created_at'   => date('Y-m-d H:i:s'),
                        'updated_at'   => date('Y-m-d H:i:s'),
                    ]);
                }

                TroublePrice::create([
                    'trouble_id'   => $chkTrouble->id,
                    'model_id'     => $brandM->id,
                    'price'        => $d->price,
                    'is_activated' => 1,
                    'is_deleted'   => 0,
                    'created_at'   => date('Y-m-d H:i:s'),
                    'updated_at'   => date('Y-m-d H:i:s'),
                ]);
            }
        }
    }
}
