<?php

namespace App\Mail\SelfPaced;

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

class SelfPacedBookDownloadMail extends Mailable
{
    use Queueable, SerializesModels;

    public SelfPacedBookPurchase $bookPurchase;

    /**
     * Create a new message instance.
     */
    public function __construct(SelfPacedBookPurchase $bookPurchase)
    {
        $this->bookPurchase = $bookPurchase;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: "Download Your Course Book — {$this->bookPurchase->enrollment->course->title}",
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        // Generate a signed download URL valid for 24 hours
        $downloadUrl = route('self-paced.learn.book.download-signed', [
            'bookPurchase' => $this->bookPurchase->id,
        ]);

        return new Content(
            view: 'emails.self-paced.book-download',
            with: [
                'bookPurchase' => $this->bookPurchase,
                'downloadUrl' => $downloadUrl,
                'expiresAt' => now()->addHours(24),
                'course' => $this->bookPurchase->enrollment->course,
                'learner' => $this->bookPurchase->enrollment->user,
            ],
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}
