<?php

namespace App\Notifications;

use App\Models\Product;
use App\Models\ProductVersion;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class ProductVersionUpdateNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(
        public Product $product,
        public ProductVersion $current,
        public ProductVersion $latest
    ) {
    }

    public function via(object $notifiable): array
    {
        // return ['mail', 'database'];
        return ['database'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        echo "ToMail Preparing email notification for user {$notifiable->id} about product {$this->product->id}\n";
        return (new MailMessage)
            ->subject("Update available for {$this->product->name}")
            ->greeting("Hi {$notifiable->name},")
            ->line("A newer base solution version is available for {$this->product->name}.")
            ->line("Current: {$this->current->version} ({$this->current->release_type})")
            ->line("Latest: {$this->latest->version} ({$this->latest->release_type})")
            ->lineIf($this->latest->note, "Note: {$this->latest->note}")
            ->lineIf($this->latest->changelog, 'Changelog:')
            ->lineIf($this->latest->changelog, strip_tags($this->latest->changelog))
            ->action('View Versions', route('admin.product-versions.index'))
            ->line('You can upgrade in the admin panel.');
    }

    public function toArray(object $notifiable): array
    {
        echo "ToArray Preparing notification data for user {$notifiable->id} about product {$this->product->id}\n";
        return [
            'product_id' => $this->product->id,
            'product_name' => $this->product->name,
            'current_version' => $this->current->version,
            'latest_version' => $this->latest->version,
            'latest_release_type' => $this->latest->release_type,
            'message' => "New base solution version {$this->latest->version} available for {$this->product->name}",
            'url' => route('admin.product-versions.index'),
        ];
    }
}
