<?php

namespace Database\Seeders;

use App\Models\WorkerRequest;
use App\Models\WorkerRequestApplication; 
use App\Models\Company; 
use App\Models\CompanyUser;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

/**
 * WorkerRequestSeeder Class must run first.
 * 
 * php artisan db:seed --class=WorkerRequestApplicationSeeder.
 */

class WorkerRequestApplicationSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $requestLimit   =    100;

        $itemsPerApplication = 
        [
            'min'   =>   1, 
            'max'   =>   10, 
        ]; 

        $workerRequests     =   WorkerRequest::query()
                                    ->select('id', 'company_id')
                                    ->inRandomOrder()
                                    ->limit($requestLimit)
                                    ->get();

        if (count($workerRequests) == 0)
        {
            $this->command->warn("There are currently no Worker Requests. please run the seeder.");
        } 

        foreach ($workerRequests as $request)
        {
            $items  =  rand($itemsPerApplication['min'], $itemsPerApplication['max']); 

            for ($i = 0; $i < $items; $i++)
            {
                //! FETCH COMPANY 
                $companyId  =   Company::query()
                                    ->select('id')
                                    ->where('id', '!=', $request->company_id)
                                    ->inRandomOrder()
                                    ->first()
                                    ?->id;

                //! FETCH WORKER
                $workerId   =   CompanyUser::query()
                                    ->select('worker_id')
                                    ->where(
                                        [
                                            ['company_id',          '=', $companyId],
                                            ['role_id',             '=', 5],
                                            ['availability_status', '=', 'for-lease'],
                                        ]
                                    )
                                    ->inRandomOrder()
                                    ->first()
                                    ?->worker_id; 
                

                //! IF COMPANY USERS, SKIP THE LOOP
                if ($workerId == null)
                {
                    $this->command->warn("Worker Request Id #{$request->id} Iteration #{$i} : Company Id #{$companyId} has no Workers available for-lease"); 

                    continue; 
                }

                //! CHECK IF RECORD ALREADY EXIST
                $recordExist    =   WorkerRequestApplication::query()
                                        ->select('id')
                                        ->where(
                                            [
                                                [ 'company_id', '=', $companyId ],
                                                [ 'worker_id',  '=', $workerId ]
                                            ]
                                        )
                                        ->count() >= 1;  

                if ($recordExist == true)
                {
                    $this->command->warn("Worker Request Id #{$request->id} Iteration #{$i} : Company Id #{$companyId} & Worker Id #{$workerId} Combination already exist."); 

                    continue; 
                }

                //! INSERT DATA
                $application = WorkerRequestApplication::create(
                    [
                        'worker_request_id'     =>  $request->id, 
                        'company_id'            =>  $companyId, 
                        'worker_id'             =>  $workerId, 
                    ]
                );

                $this->command->info("Worker Request Id #{$request->id} Iteration #{$i} : Application Id #{$application->id} Created"); 
            }
        } 
    }
}
