<?php

namespace App\Mail;

use Carbon\Carbon;
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 InvoiceTransitMail extends Mailable
{
    use Queueable, SerializesModels;

    public $scheduleType;

    public $tableData;

    public $mailSubject;

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

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

    /**
     * Get the message content definition.
     */
    // public function content(): Content
    // {
    //     return new Content(
    //         view: 'view.name',
    //     );
    // }

    public function content(): Content
    {
        $greeting = '<b>Dear Service Provider</b>,<br><br>The following consignments to our branch warehouse are overdue beyond the agreed timeline.Immediate delivery is required today; Failure to complain will result to not able to fulfill customer requirements and leads to potential sales loss.Please treat this as the critical and confirm despatch without delay. <br><br><b> Dear C.R.I Branch Team,</b> <br><br> Please follow and ensure the same.';

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

          if (strtolower($this->scheduleType) == 'daily') {

            $dates = collect($this->tableData)
                ->pluck('lr_bl_aw_date')
                ->filter()
                ->map(fn ($d) => Carbon::parse($d));

            if ($dates->isNotEmpty()) {
                $startDate = $dates->min()->startOfDay();
            }
            else
            {
                $startDate = now()->subDay()->startOfDay();
            }

            $endDate = now();

            // $reportPeriod = "<br><br>"
            //     . "Report Period: <b>{$startDate->format('d-m-Y')}</b> "
            //     . "to <b>{$endDate->format('d-m-Y')}.</b>";
                // . "<br>Please arrange to receipt the same immediately.";

            //$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.invoice-in-transit-report',
            with: [
                'company' => 'CRI Digital Manufacturing Solutions',
                'greeting' => $greeting,
                'tableData' => $this->tableData,
                '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 [];
    }
}
