<?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 OrderConfirmed extends Mailable
{
    use Queueable, SerializesModels;
    public $customername;
    public $orderId;

    public $productId;

    public $qty;

    public $color,$orderItems,$totalOrderAmount,$totalServiceFee;

    public $total;
    public $unitprice;
    public $servicefee;
    public $finalamount;


    public $customerphone;
    public $customeraddress;
    public $customeraddresstwo;
    public $orderdate;
    public $taxAmount;
    public $taxPercentage;
    public $discountAmount;
    public $finalam;
    public $pdfData;
    public $invoice;
    public $pointsApplied;
    /**
     * Create a new message instance.
     */
    public function __construct($customername,$orderId,$orderItems,$totalOrderAmount,$totalServiceFee,$customerphone,$customeraddress,$customeraddresstwo,$orderdate, $taxAmount = 0, $taxPercentage = 0, $discountAmount = 0, $invoice = null, $pointsApplied = 0)
    {
        
      $this->customername = $customername;
        $this->orderId = $orderId;
        $this->orderItems = $orderItems;
        $this->totalOrderAmount = $totalOrderAmount;
        $this->totalServiceFee = $totalServiceFee;
        $this->customerphone = $customerphone;
        $this->customeraddress = $customeraddress;
        $this->customeraddresstwo = $customeraddresstwo;
        $this->orderdate = $orderdate;
        $this->taxAmount = $taxAmount;
        $this->taxPercentage = $taxPercentage;
        $this->discountAmount = $discountAmount;

        $this->finalam= $this->totalOrderAmount;
        $this->invoice = $invoice;
        $this->pointsApplied = $pointsApplied;
    }

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

    /**
     * Get the message content definition.
     */
    public function build()
    {
        return $this->subject('Order Successfully Placed – American Plastics')->view('emails.orderconfirmed', [
           'customername' => $this->customername,
                'orderid' => $this->orderId,
                'orderItems' => $this->orderItems,
                'totalOrderAmount' => $this->totalOrderAmount,
                'totalServiceFee' => $this->totalServiceFee,
                'customerphone' => $this->customerphone,
                'customeraddress' => $this->customeraddress,
                'customeraddresstwo' => $this->customeraddresstwo,
                'orderdate' => $this->orderdate,
                'taxAmount' => $this->taxAmount,
                'taxPercentage' => $this->taxPercentage,
                'discountAmount' => $this->discountAmount,
                'finalamount' =>$this->totalServiceFee,
                'pointsApplied' => $this->pointsApplied,

        ]);
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        if ($this->invoice) {
            try {
                $pdf = \Barryvdh\DomPDF\Facade\Pdf::loadView('admin.invoices.pdf', ['invoice' => $this->invoice]);
                return [
                    \Illuminate\Mail\Mailables\Attachment::fromData(fn () => $pdf->output(), 'Invoice-' . $this->invoice->invoice_number . '.pdf')
                        ->withMime('application/pdf'),
                ];
            } catch (\Exception $e) {
                \Illuminate\Support\Facades\Log::error('PDF Attachment Generation Failed: ' . $e->getMessage());
            }
        }
        return [];
    }
}
