<?php

namespace App\Mail;

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

class CourseNudgeMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(public Enrollment $enrollment)
    {
    }

    public function envelope(): Envelope
    {
        $course = $this->enrollment->course;
        $progress = (int) $this->enrollment->progress;

        $subject = $progress > 0
            ? "You're {$progress}% through {$course->title}, let's finish"
            : "Your first lesson in {$course->title} is waiting";

        return new Envelope(subject: $subject);
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.course-nudge',
            with: [
                'firstName' => trim(explode(' ', (string) $this->enrollment->user->name)[0] ?? ''),
                'course' => $this->enrollment->course,
                'progress' => (int) $this->enrollment->progress,
                'url' => route('learn.show', $this->enrollment->course),
            ],
        );
    }
}
