<?php

namespace App\Mail;

use App\Mail\Concerns\RidesALane;
use App\Mail\Concerns\SendsFromMailbox;
use App\Models\UserDevice;
use App\Services\Mail\MailCatalogue;
use App\Services\Mail\MailPurpose;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

/**
 * "Somebody signed in from a device this account has not used before."
 *
 * Sent from the SYSTEM mailbox rather than SUPPORT: it is not a conversation,
 * and a reply to it should not open a ticket.
 *
 * The link goes to the session list, because a warning whose only advice is
 * "if this was not you, take action" is a warning that names a problem and
 * hands back nothing to do about it.
 */
class NewDeviceSignInMail extends Mailable implements ShouldQueue
{
    use Queueable, RidesALane, SendsFromMailbox, SerializesModels;

    public function __construct(
        public string $name,
        public UserDevice $device,
        public string $guard,
    ) {
        $this->mailer(MailCatalogue::mailer('new-device'));
    }

    public function envelope(): Envelope
    {
        return $this->mailboxEnvelope(
            MailPurpose::SYSTEM,
            __('devices.mail_subject'),
            'new-device',
        );
    }

    public function content(): Content
    {
        return new Content(view: 'mail.new-device', with: [
            'name' => $this->name,
            'device' => $this->device,
            // Operators and customers land in different places, and sending
            // somebody to the other product's settings page is sending them
            // somewhere they cannot sign in (R21).
            'sessionsUrl' => $this->guard === 'operator'
                ? route('admin.settings')
                : route('settings'),
        ]);
    }
}
