<?php

namespace App\Console\Commands;

use App\Models\Milestone;
use App\Models\Proposal;
use App\Notifications\MilestoneDueDate;
use App\Notifications\PayMilestone;
use Carbon\Carbon;
use Illuminate\Console\Command;

class MileStoneEmails extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'milestone:due';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends Daily reminders for user due to milestones';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        foreach (Milestone::whereBetween('start_date',[Carbon::now(), Carbon::now()->addDays(5)])->get() as $milestone){
            if($milestone->transaction ==null){
                $milestone->milestonable->getModel() instanceof Proposal ?
                    $milestone->milestonable->project->user->notify(new PayMilestone($milestone)) :
                    $milestone->milestonable->user->notify(new PayMilestone($milestone));
            }
        }
        foreach (Milestone::whereBetween('due_date',[Carbon::now(), Carbon::now()->addDays(5)])->where('accepted_at','=',null)->get() as $milestone){
            $milestone->milestonable->getModel() instanceof Proposal ?
                $milestone->milestonable->proposalable->notify( new MilestoneDueDate($milestone)) :
                $milestone->milestonable->service->serviceable->notify( new MilestoneDueDate($milestone));
        }
    }
}
