<?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;

// class InvalidSerialMail extends Mailable
// {
//     use Queueable, SerializesModels;

//     /**
//      * Create a new message instance.
//      */
//    public $serial;
//    public $invoiceNumber;
//    public $plantName;
//    public $invoiceType;

//     public $greetings;


//     public function __construct($serial, $invoiceNumber, $plantName, $invoiceType)
//     {
//         $this->serial = $serial;
//         $this->invoiceNumber = $invoiceNumber;
//         $this->plantName = $plantName;
//         $this->invoiceType = $invoiceType;
//     }

//     /**
//      * Get the message envelope.
//      */
//     public function envelope(): Envelope
//     {
//         return new Envelope(
//             subject: 'Invalid Serial Mail',
//         );
//     }

//     /**
//      * Get the message content definition.
//      */
//     public function content(): Content
//     {
//         $greeting = "Dear Sir/Madam,<br><br>Please note that the scanned serial number format appears to be incorrect.";

//         return new Content(
//             view: 'mail.invalid-serial',
//             with: [
//                 'company' => "CRI Digital Manufacturing Solutions",
//                 'greeting' => $greeting,
//                 'wishes' => "Thanks & Regards,<br>CRI Digital Manufacturing Solutions"
//             ],
//         );
//     }

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

class InvalidSerialMail extends Mailable
{
    use Queueable, SerializesModels;

    public $serial;
    public $invoiceNumber;
    public $mplantName;
    public $mInvoiceType;
    public $mailType;     // new
    public $greeting;
    public $subjectLine;

    public $itemCode;

    /**
     * Create a new message instance.
     */
    public function __construct($serial, $invoiceNumber, $mplantName, $mInvoiceType, $itemCode, $mailType = 'InvalidFormat')
    {
        $this->serial = $serial;
        $this->invoiceNumber = $invoiceNumber;
        $this->mplantName = $mplantName;
        $this->mInvoiceType = $mInvoiceType;
        $this->mailType = $mailType;
        $this->itemCode = $itemCode;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        // dynamic subject based on mail type
        switch ($this->mailType) {
            case 'NotFound':
                $this->subjectLine = "Serial Number Not Found ({$this->mplantName})";
                break;
            case 'NotFoundInvoice':
                $this->subjectLine = "Serial Number Not Found in Invoice ({$this->mplantName})";
                break;
            case 'NotFoundItemS':
                $this->subjectLine = "Item Code Not Found ({$this->mplantName})";
                break;
            case 'NotValidPackage':
                $this->subjectLine = "Not Valid Package ({$this->mplantName})";
                break;
            case 'InvalidMaterialFormat':
                $this->subjectLine = "Invalid Qr code format Found material ({$this->mplantName})";
                break;
            case 'DuplicateMotorQR':
                $this->subjectLine = "Duplicate Serial Number ({$this->mplantName})";
                break;
            case 'NotMotorQR':
                $this->subjectLine = "Item Code doesn't have Motor QR ({$this->mplantName})";
                break;
            case 'CompletedSerialInvoice':
                $this->subjectLine = "Completed Serial Invoice ({$this->mplantName})";
                break;
            case 'NotPumpQR':
                $this->subjectLine = "Item Code doesn't have Pump QR ({$this->mplantName})";
                break;
            case 'DuplicatePumpQR':
                $this->subjectLine = "Duplicate Serial Number ({$this->mplantName})";
                break;
            case 'CSerialInvoice':
                $this->subjectLine = "Completed Serial Invoice ({$this->mplantName})";
                break;
            case 'MissingPanelBox':
                $this->subjectLine = "Missing Panel Box ({$this->mplantName})";
                break;
            case 'DuplicateCapacitorQR':
                $this->subjectLine = "Duplicate Capacitor QR ({$this->mplantName})";
                break;
            case 'UnknownPumpsetQR':
                $this->subjectLine = "Unknown PumpSet QR ({$this->mplantName})";
                break;
            case 'DuplicatePumpsetQR':
                $this->subjectLine = "Duplicate PumpSet QR ({$this->mplantName})";
                break;
            case 'ComSerInv':
                $this->subjectLine = "Completed Serial Invoice ({$this->mplantName})";
                break;
            case 'InvalidFormat':
            default:
                $this->subjectLine = "Invalid Serial Format Found ({$this->mplantName})";
                break;
        }

        return new Envelope(
            subject: $this->subjectLine,
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        // dynamic greeting/message body
        switch ($this->mailType) {
            case 'NotFound':
                $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    The scanned Serial Number <b>{$this->serial}</b> was not found in the database
                    for the plant <b>{$this->mplantName}</b>.<br><br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br><br>
                    Please verify the serial number.
                ";
                break;
            case 'NotFoundInvoice':
                $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    The scanned Serial Number <b>'{$this->serial}'</b> was not found in the Invoice Number <b>'{$this->invoiceNumber}'</b>
                    for the plant <b>{$this->mplantName}</b>.<br><br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br><br>
                    Please verify the serial number.
                ";
                break;
            case 'NotFoundItemS':
                $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Item Code <b>'{$this->itemCode}'</b> with Serial Number<b>'{$this->serial}'</b> not found in sticker master
                    for the plant <b>{$this->mplantName}</b>.<br><br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br><br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br><br>
                    Please verify the Item Code.
                ";
                break;
            case 'NotValidPackage':
                $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned Item Code<b>'{$this->itemCode}'</b> doesn't have valid package type to proceed!
                    for the plant <b>{$this->mplantName}</b>.<br><br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br><br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br><br>
                    Please verify the Item Code.
                ";
                break;
            case 'InvalidMaterialFormat':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Please note that the scanned serial number format appears to be incorrect.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'DuplicateMotorQR':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned 'Motor' serial number <b>{$this->serial}</b> already completed the scanning process.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'NotMotorQR':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned Item Code <b>{$this->itemCode}</b> doesn't have 'Motor' QR to proceed!<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'CompletedSerialInvoice':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Serial invoice <b>'{$this->invoiceNumber}'</b> completed the scanning process.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'NotPumpQR':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned Item Code <b>'{$this->itemCode}'</b> doesn't have 'Pump' QR to proceed!<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'DuplicatePumpQR':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned 'Pump' serial number already completed the scanning process.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'CSerialInvoice':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Serial invoice <b>'{$this->invoiceNumber}'</b> completed the scanning process.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'MissingPanelBox':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned Item Code <b>'{$this->itemCode}'</b> doesn't have 'Panel Box Code' to proceed!<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'DuplicateCapacitorQR':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned 'Capacitor' serial number already completed the scanning process.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'UnknownPumpsetQR':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned Item Code <b>'{$this->itemCode}'</b> doesn't have 'Pump Set' QR to proceed!<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'DuplicatePumpsetQR':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Scanned 'Pump Set' serial number already completed the scanning process.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'ComSerInv':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Serial invoice <b>'{$this->invoiceNumber}'</b> completed the scanning process.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'ItemNotFound':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Item code <b>'{$this->itemCode}'</b> not found in database.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'ItemNotFoundDB':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Item code <b>'{$this->itemCode}'</b> not found in database for choosed plant.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'ItemNotValidMaterialType':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Item code <b>'{$this->itemCode}'</b> doesn't have a valid material type.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'ItemNotInvoice':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Item code <b>'{$this->itemCode}'</b> doesn't exist in invoice.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'ItemNotInvoice':
                 $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Item code <b>'{$this->itemCode}'</b> doesn't exist in invoice.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}<br>
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
            case 'InvalidFormat':
            default:
                $this->greeting = "
                    Dear Sir/Madam,<br><br>
                    Please note that the scanned serial number format appears to be incorrect.<br>
                    <b>Plant:</b> {$this->mplantName}<br>
                    <b>Invoice Type:</b> {$this->mInvoiceType}
                    <b>Invoice Number:</b> {$this->invoiceNumber}<br>
                    <b>Scanned QR Code:</b> {$this->serial}<br>
                ";
                break;
        }

        return new Content(
            view: 'mail.invalid-serial',
            with: [
                'company' => "CRI Digital Manufacturing Solutions",
                'greeting' => $this->greeting,
                'wishes' => "Thanks & Regards,<br>CRI Digital Manufacturing Solutions",
            ],
        );
    }
}
