<?php

namespace App\Mail;

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

class VideoShared extends Mailable
{
    use Queueable, SerializesModels;

    public string $shareTitle;

    public function __construct(
        public Video $video,
        public string $shareUrl,
        public User $sender,
        public ?string $personalMessage = null,
        ?string $shareTitle = null,
    ) {
        // Version-aware title (e.g. the English track's title) with a primary fallback.
        $this->shareTitle = $shareTitle ?: $video->title;
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: $this->sender->name . ' shared "' . $this->shareTitle . '" with you on ' . config('app.name'),
            // Let the recipient reply straight to the friend who sent it.
            replyTo: $this->sender->email
                ? [new Address($this->sender->email, $this->sender->name)]
                : [],
        );
    }

    public function content(): Content
    {
        return new Content(view: 'emails.video-shared');
    }
}
