<?php

namespace App\Livewire\Components;

use App\Models\Project;
use Livewire\Attributes\On;
use Livewire\Component;

class DownloadNotification extends Component
{
    public $show_download_notification;
    public Project $project;
    // TODO Change session bool to timestamp so i can check if i show it in 5 min.
    public function mount(Project $project)
    {
        if (auth()->user()->hasRole('manager')) {
            $this->show_download_notification = session()->get('show-download.'.$project->id,
                $project->hasMissingDownloadsDateInHarvestFile());
            $this->project = $project;
        }
    }

    public function getSessionKey()
    {
        return 'show-download.'.$this->project->id;
    }

    #[On('download_notify')]
    public function open()
    {
        if (auth()->user()->hasRole('manager')) {
            $this->show_download_notification = true;
            session([$this->getSessionKey() => true]);
        }
    }
    public function close(): void
    {
        $this->show_download_notification = false;
        session([$this->getSessionKey()=>false]);
    }

    public function startDownloads()
    {
        $this->project->handleMissingDownloads();
        $this->close();
        $this->dispatch('notify',message:'Downloads added to the queue.',type:'success');
    }


}
