<?php

namespace App\Livewire;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use App\Models\ZEmail;
use Livewire\Component;

class Home extends Component
{
    protected $listeners = ['fetchMessages' => 'checkIfAnyMessage'];

    public function checkIfAnyMessage()
    {
        $email = ZEmail::getEmail();
        $messages = ZEmail::getMessages($email);
        if (count($messages['data']) > 0) {
            return to_route('mailbox');
        }
        return null;
    }

    public function render(): Factory|View
    {
        return view('livewire.home');
    }
}
