<?php

declare(strict_types=1);

namespace App\Mail;

use App\Facades\Settings;
use App\Facades\Site;
use App\Traits\GetsColourSettings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Mtc\MercuryDataModels\SiteAccessRequest;

class SiteAccessLinkMail extends Mailable implements ShouldQueue
{
    use Queueable;
    use SerializesModels;
    use GetsColourSettings;

    public function __construct(
        public readonly SiteAccessRequest $accessRequest,
    ) {
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address(
                Settings::get('mail-from-email') ?: config('mail.from.address'),
                Settings::get('mail-from-name') ?: config('mail.from.name'),
            ),
            subject: __('Your access link for :site', ['site' => Settings::get('app-name')]),
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.site-access-link',
            with: [
                'site_name' => Settings::get('app-name'),
                'site_logo' => Settings::get('site-logo') ?? ['url' => null],
                'site_url' => Site::url(''),
                'access_url' => Site::url('/access-granted?token=' . $this->accessRequest->token),
                'expires_at' => $this->accessRequest->expires_at,
                'colours' => $this->getColourSettings(),
            ],
        );
    }
}
