<?php

namespace Database\Seeders;

use App\Models\Certificate;
use App\Models\Contract;
use App\Models\Education;
use App\Models\Engineer;
use App\Models\Experience;
use App\Models\Firm;
use App\Models\Language;
use App\Models\Location;
use App\Models\Message;
use App\Models\PortfolioProject;
use App\Models\Project;
use App\Models\Proposal;
use App\Models\Skill;
use App\Models\Skillable;
use App\Models\SocialLink;
use App\Models\User;
use Carbon\Carbon;
use Faker\Generator;
use Illuminate\Container\Container;
use Illuminate\Database\Seeder;

class TestAccountSeeder extends Seeder
{
    /**
     * The current Faker instance.
     *
     * @var \Faker\Generator
     */
    protected $faker;

    /**
     * Create a new seeder instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->faker = $this->withFaker();
    }

    /**
     * Get a new Faker instance.
     *
     * @return \Faker\Generator
     */
    protected function withFaker()
    {
        return Container::getInstance()->make(Generator::class);
    }
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::factory()->create([
            'name' => 'Abdulrhman Elkayal',
            'email' => 'aelkayal88@gmail.com',
            'password' => bcrypt('12345678'),
        ]);

        $user = User::where('email', 'aelkayal88@gmail.com')->first();
//        $user->projects()->saveMany(Project::factory(1)->create());
       $engineer= Engineer::factory(1)->create([
            'user_id' => $user->id,
            'email'=>"muhammedkamal022@gmail.com"
        ]);

        Firm::factory(1)->create([
            'user_id' => $user->id,
            'email'=>"aelkayal88@gmail.com"
        ]);

//        $engineer = Engineer::where('user_id', $user->id)->first();
//
//        for ($i=0; $i < 5; $i++) {
//
//            $project = Project::all()->random();
//
//            $project->proposals()->saveMany(Proposal::factory(5)->create([
//                'proposalable_id' => $engineer->id,
//                'proposalable_type' => 'App\Models\Engineer',
//                'project_id'=>$project->id,
//                'accepted'=>false,
//            ]));
//
//            $acceptedProp = $project->proposals()->save(Proposal::factory()->create([
//                'proposalable_id' => $engineer->id,
//                'proposalable_type' => 'App\Models\Engineer',
//                    'project_id'=>$project->id,
//                    'accepted'=>true,
//                ]));
//
//            $contractorSignedAtt = $this->faker->dateTimeBetween('-300 days');
//
//            $project->contract()->save(Contract::create([
//                'project_id'=>$project->id,
//                'user_id'=>$project->user_id,
//                'contractor_id' => $acceptedProp->proposalable_id,
//                'contractor_type' => $acceptedProp->proposalable_type,
//                'amount'=>$acceptedProp->amount,
//                'paid_amount' => $this->faker->numberBetween(0,$acceptedProp->amount),
//                'proposal_id' => $acceptedProp->id,
//                'contractor_signed_at' => $contractorSignedAtt,
//                'user_signed_at' => $this->faker->dateTimeBetween('-350 days'),
//                'start_date' => $contractorSignedAtt,
//                'due_date' => Carbon::createFromTimestamp($contractorSignedAtt->getTimestamp())->addDays($acceptedProp->duration),
//            ]));
//
//        }

        $user->assignRole('super-admin');

        $user = User::where('email', 'admin@dniks.com')->first();
        $user->assignRole('super-admin');


//        $chat= $engineer->chats()->create([
//            "chatable_type" => "App\Models\Project",
//            "chatable_id"=>$project->id,
//            "user_id"=>$user->id,
//        ]);
//        $chat->messages()->saveMany(Message::factory(5)->create());
//
//
//        $project = Project::inRandomOrder()->first();
//        $user = $project->user;
//
//        $chat = $engineer->chats()->create([
//            "chatable_type" => "App\Models\Project",
//            "chatable_id"=>$project->id,
//            "user_id"=>$user->id,
//        ]);
//        $chat->messages()->saveMany(Message::factory(15)->create());
//
//        $project = Project::inRandomOrder()->first();
//        $user = $project->user;
//
//        $chat = $engineer->chats()->create([
//            "chatable_type" => "App\Models\Project",
//            "chatable_id"=>$project->id,
//            "user_id"=>$user->id,
//        ]);
//        $chat->messages()->saveMany(Message::factory(12)->create());

        // Education::factory(3)->create([
        //     'engineer_id' => $engineer->id
        // ]);

        // Experience::factory(3)->create([
        //     'engineer_id' => $engineer->id
        // ]);

        // Certificate::factory(5)->create([
        //     'engineer_id' => $engineer->id
        // ]);

        // PortfolioProject::factory(2)->create([
        //     'engineer_id' => $engineer->id
        // ]);

        // Location::factory(1)->create([
        //     'locationable_id' =>$engineer->id,
        //     'locationable_type' => 'App\Models\Engineer'
        // ]);

        // Project::factory(5)->create([
        //     'projectable_id' =>$engineer->id,
        //     'projectable_type' => 'App\Models\Engineer'
        // ]);

        // // Language::factory(5)->create([
        // //     'languagable_id' =>$engineer->id,
        // //     'languagable_type' =>'engineer'
        // // ]);

        // Skillable::factory(15)->create([
        //     'skillable_id' =>$engineer->id,
        //     'skillable_type' =>'App\Models\Engineer'
        // ]);

    }
}
