<?php

namespace App\Console\Commands;

use App\Models\Ticket;
use App\Notifications\Ticket\FollowUpReminderNotification;
use Illuminate\Console\Command;

class FollowupReminder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cp:followup-reminder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send a follow-up reminder email to users who have not responded to their support ticket.';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $tickets = Ticket::where('status', 'Open')
            ->whereHas('messages', function ($query) {
                $query->whereColumn('user_id', '!=', (new Ticket)->getTable() . '.user_id')
                    ->where('updated_at', '<', now()->subDays(3));
            })
            ->get();

        foreach ($tickets as $ticket) {
            $ticket->user->notify(new FollowUpReminderNotification($ticket));
            $this->info("Reminder sent to user for ticket [$ticket->reference_id]");
        }

        $this->info('Reminder notifications sent successfully!');
    }
}
