<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\DatabaseMessage;

class ImportCompletedNotification extends Notification
{
    use Queueable;

    protected $results;

    public function __construct($results)
    {
        $this->results = $results;
    }

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

    public function toMail($notifiable)
    {
        $successCount = $this->results['success_count'] ?? 0;
        $errorCount = $this->results['error_count'] ?? 0;
        $updatedCount = $this->results['updated_count'] ?? 0;
        $skippedCount = $this->results['skipped_count'] ?? 0;

        return (new MailMessage)
            ->subject('Import Excel Terminé')
            ->greeting('Bonjour ' . $notifiable->name)
            ->line('L\'import Excel a été terminé avec succès.')
            ->line('Résultats:')
            ->line("- Enregistrements créés: $successCount")
            ->line("- Enregistrements mis à jour: $updatedCount")
            ->line("- Enregistrements ignorés: $skippedCount")
            ->line("- Erreurs: $errorCount")
            ->line('Merci d\'utiliser notre application!');
    }

    public function toDatabase($notifiable)
    {
        return new DatabaseMessage([
            'success_count' => $this->results['success_count'] ?? 0,
            'error_count' => $this->results['error_count'] ?? 0,
            'updated_count' => $this->results['updated_count'] ?? 0,
            'skipped_count' => $this->results['skipped_count'] ?? 0,
            'errors' => $this->results['errors'] ?? [],
        ]);
    }
}
