<?php

namespace App\Mail;

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

class JobApplicationSubmitted extends Mailable
{
    use Queueable, SerializesModels;

    public $jobApplication;

    public function __construct(JobApplication $jobApplication)
    {
        $this->jobApplication = $jobApplication;
    }

    public function envelope()
    {
        return new Envelope(
            subject: 'New Job Application: ' . $this->jobApplication->jobPosting->title,
        );
    }

    public function content()
    {
        return new Content(
            view: 'emails.job-application-submitted',
            with: [
                'jobApplication' => $this->jobApplication,
            ],
        );
    }

    public function attachments()
    {
        $attachments = [];
        
        if ($this->jobApplication->resume_path && file_exists(storage_path('app/' . $this->jobApplication->resume_path))) {
            $attachments[] = Attachment::fromPath(storage_path('app/' . $this->jobApplication->resume_path))
                ->as('resume.' . pathinfo($this->jobApplication->resume_path, PATHINFO_EXTENSION));
        }
        
        return $attachments;
    }
}