<?php

namespace App\Console\Commands;

use App\Classes\Notifications\InvoiceIssuedEmail;
use App\Classes\Notifications\ShipmentDepartureEmail;
use App\Http\Helpers\General;
use App\Models\Order;
use Carbon\Carbon;

use App\Models\Container;
use Illuminate\Console\Command;

class SendContainerDepartureEmailCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */

    protected $signature = 'command:sendContainerDepartureEmailCommand';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $containers = Container::whereHas('transports', function ($transport){
           return $transport->whereDate('dispatch_date', '=', Carbon::today());
        })->get();

        foreach ($containers as $container){
            foreach ($container->packingLists as $packingList){
                if(!($packingList->owner instanceof Order)) continue;
                $user = $packingList->owner->companyModule->employees()->first();
                if(app()->environment(['production'])) {
                    $user->notify(new ShipmentDepartureEmail($user, $packingList));
                }
            }
        }


    }
}
