<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\URL;
use Illuminate\Mail\Mailables\Attachment;
use Storage;

class CharacteristicApprovalMail extends Mailable
{
    use Queueable, SerializesModels;

    public $request;
    public $approverName;
    public $level;

    public $pdfPath;
    public $tableData;

    public $pendingApprovers;

    public $approverNameFromMaster;

    public $subjectLine;

    public $characteristics;

    /**
     * Create a new message instance.
     */
    public function __construct($request, $approverName, $level, $pdfPath = null, $pendingApprovers, $approverNameFromMaster, $subjectLine, $characteristics = [])
    {
        $this->request = $request;
        $this->approverName = $approverName;
        $this->level = $level;
        $this->pdfPath = $pdfPath;
        $this->pendingApprovers = $pendingApprovers;
        $this->approverNameFromMaster = $approverNameFromMaster;
        $this->subjectLine = $subjectLine;
        $this->tableData = $characteristics;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: $this->subjectLine,
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'mail.characteristic-approval',
            with: [
                'company'    => 'CRI Digital Manufacturing Solutions',
                'greeting'   => 'Dear ' . $this->approverName . ',',
                'request'    => $this->request,
                'level'    => $this->level,
                'tableData'  => $this->tableData,
                'pendingApprovers'  => $this->pendingApprovers,
                'approverNameFromMaster'  => $this->approverNameFromMaster,
                'approveUrl' => $this->approveUrl(),
                'holdUrl'    => $this->holdUrl(),
                'rejectUrl'  => $this->rejectUrl(),
                'wishes'     => 'Thanks & Regards,<br>CRI Digital Manufacturing Solutions',
            ]
        );
    }

    protected function approveUrl()
    {
        return URL::signedRoute('characteristic.approve', [
            'id'    => $this->request->id,
            'level' => $this->level
        ]);
    }

    protected function holdUrl()
    {
        return URL::signedRoute('characteristic.hold', [
            'id'    => $this->request->id,
            'level' => $this->level
        ]);
    }


    protected function rejectUrl()
    {
        return URL::signedRoute('characteristic.reject', [
            'id'    => $this->request->id,
            'level' => $this->level
        ]);
    }

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

        if (! Storage::disk('local')->exists($this->pdfPath)) {
            return [];
        }

        return [
            Attachment::fromStorageDisk(
                'local',
                $this->pdfPath
            )->as(basename($this->pdfPath)),
        ];
    }



}
