<?php

namespace App\Mail;

use App\Models\Tenant\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class UserInvitationMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public User $user,
        public string $rawToken,
    ) {}

    public function envelope(): Envelope
    {
        $company = $this->user->company?->display_name ?? $this->user->company?->name ?? 'AutoCRM';
        return new Envelope(
            subject: "Invitație de acces — {$company}",
        );
    }

    public function content(): Content
    {
        $company = $this->user->company;
        return new Content(
            view: 'emails.user-invitation',
            with: [
                'name' => $this->user->name,
                'invitedBy' => $this->user->invitedBy?->name ?? 'Echipa',
                'companyName' => $company?->display_name ?? $company?->name ?? 'AutoCRM',
                'acceptUrl' => url('/invitations/' . $this->rawToken),
            ],
        );
    }
}
