<?php

namespace App\Notifications;

use App\Models\NotificationLog;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class WelcomeNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function via(object $notifiable): array
    {
        if (! Setting::get('notify_welcome', true)) {
            return [];
        }
        return ['mail'];
    }

    public function toMail(User $notifiable): MailMessage
    {
        $this->logNotification($notifiable);

        return (new MailMessage)
            ->subject('Welcome to JAM Members Club!')
            ->greeting("Welcome, {$notifiable->first_name}!")
            ->line('Thank you for joining the JAM Members Club. Your membership is now active.')
            ->line("Your membership number is **{$notifiable->membership_number}**.")
            ->line('As an **Invited** tier member, you can start earning points with every qualifying visit to JAM Corner.')
            ->line('Here\'s how it works:')
            ->line('• Spend $100+ per visit to earn points (1 point per dollar)')
            ->line('• Accumulate points to unlock higher tiers: Indulged & Limitless')
            ->line('• Enjoy exclusive benefits at each tier level')
            ->action('View Your Dashboard', route('member.dashboard'))
            ->line('We look forward to welcoming you at JAM Corner!')
            ->salutation('Warm regards, The JAM Corner Team');
    }

    private function logNotification(User $user): void
    {
        NotificationLog::create([
            'user_id' => $user->id,
            'type'    => 'welcome',
            'subject' => 'Welcome to JAM Members Club!',
            'status'  => 'sent',
            'sent_at' => now(),
        ]);
    }
}
