<?php

namespace App\Mail;

use App\Models\ProjectReport;
use App\ProjectLogger;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class ReportNotFound extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: sprintf('Report Not Found for project %s', $this->report->project->name)
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'emails.report-not-found',
            with: [
                'logoPath'=> resource_path('images/smartcane.png'),
            ],
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        $path = ProjectLogger::getLogFileForToday($this->report->project);
        if ($path) {
            $filename = sprintf('%s_%s.log', $this->report->project->name, now()->format('Ymd'));
            return [
                Attachment::fromPath($path)
                    ->as($filename)
                    ->withMime('text/plain'),
            ];
        }
        return [];
    }
}
