<?php

namespace App\Mail;

use DateTime;
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\Mail\Mailables\Attachment;

class ImportTransitMail extends Mailable
{
    use Queueable, SerializesModels;

    public $scheduleType;

    public $tableData;

    public $mailSubject;

    public $excelPath;

    public $name;

    public $mobileNo;

    /**
     * Create a new message instance.
     */
    public function __construct($scheduleType, $tableData, $mailSubject, $excelPath, $name, $mobileNo)
    {
        $this->scheduleType = $scheduleType;
        $this->tableData = $tableData ?? [];
        $this->mailSubject = $mailSubject ?? 'Import Transit';
        $this->excelPath = $excelPath;
        $this->name = $name;
        $this->mobileNo = $mobileNo;
    }

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

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        $now = now();
        $reportDate = $now->format('d/m/Y');
        $greeting = '<b>Dear Sir/Madam</b>,<br>Kindly find attached the Pending Import Shipment Status Report as on ' . $reportDate . ' for your reference.<br>We request you to review the shipments highlighted in <span style="background-color: yellow; font-weight: bold;">yellow</span> and arrange for their clearance at the earliest to avoid further delays and additional charges.<br>Please note that for shipments pending under <b>Telex Release</b> and <b>Duty Payment</b>, CFS charges will be commenced post ETA. We therefore urge you to expedite the necessary actions at your end.<br>FCL shipment will take 5-6 Days for clearance & LCL shipment will take 7-8 Days for clearance after arrival of the shipment @ destination Port.</br>';

        //$greeting1 = 'Dear C.R.I Branch Team, <br><br> Please follow and ensure the same';

        if ($this->scheduleType == 'daily') {
            $reportPeriod = "The following report presents results";
            $greeting .= $reportPeriod;
        }

        if ($this->scheduleType == 'Hourly') {
            $now = now();
            $fromHour = $now->copy()->subHour()->format('H:i:s');
            $toHour = $now->format('H:i:s');
            $reportDate = $now->format('d/m/Y');
            $greeting .= "from: $reportDate, $fromHour to $toHour. <br><br>Please arrange to receipt the same immediately.";
        }

        if ($this->scheduleType == 'Live') {
            $now = now();
            $fromMinute = $now->copy()->subMinute()->format('d/m/Y H:i:s');
            $toMinute = $now->format('d/m/Y H:i:s');
            $greeting .= "from: $fromMinute to $toMinute. <br><br>Please arrange to receipt the same immediately.";
        }

        return new Content(
            view: 'mail.import_transit_report',
            with: [
                'company' => 'CRI Digital Manufacturing Solutions',
                'greeting' => $greeting,
                'tableData' => $this->tableData,
                'wishes' => "Thanks & Regards,<br>CRI Digital Manufacturing Solutions<br><b>Updated By</b> - $this->name<br>For any queries, please contact the number below<br>+91 $this->mobileNo",
            ],
        );
    }


    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [
            Attachment::fromStorageDisk(
                'local',
                $this->excelPath
            )->as('Import_Transit_Report.xlsx')
            ->withMime(
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ),
        ];
    }
}
