<?php

namespace App\Mail;

use DateTime;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

// use App\Exports\ProductionExport;
// use Illuminate\Mail\Mailables\Attachment;
// use Maatwebsite\Excel\Facades\Excel;

class ProductionMail extends Mailable
{
    use Queueable, SerializesModels;

    public $tableData;

    public $scheduleType;
    // public $excelData;
    // public $dates;

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

    // public function __construct($scheduleType,$tableData = [],$excelData = [],$dates = [])
    // {
    //     $this->scheduleType = $scheduleType;
    //     $this->tableData    = $tableData;
    //     $this->excelData    = $excelData;
    //     $this->dates        = $dates;
    // }

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

    /**
     * Get the message content definition.
     */
    // public function content(): Content
    // {
    //      return new Content(
    //         view: 'mail.production_report',
    //         with: [
    //             'company' => "CRI Digital Manufacturing Solutions",
    //             'greeting' => "Dear Sir/Madam,<br><br>Kindly find the attached production report status details for the 'Target Quantity' and 'Production Quantity' count,",
    //             // 'greeting' => "Dear Sir/Madam,<br><br>Kindly find the attached production report status details for the 'Target Quantity' and 'Production Quantity' count,<br>" . $reportPeriod,
    //              'tableData' => $this->tableData,
    //              'wishes' => "Thanks & Regards,<br>CRI Digital Manufacturing Solutions"
    //         ],
    //     );
    // }

    public function content(): Content
    {
        $greeting = "Dear Sir/Madam,<br><br>Kindly find the attached production report status details for the 'Target Quantity' and 'Production Quantity' count,";

        if ($this->scheduleType == 'Daily') {
            $fromDate = (new DateTime('yesterday 08:00'))->format('d/m/Y H:i').':000';
            $toDate = (new DateTime('today 07:59'))->format('d/m/Y H:i').':999';
            $reportPeriod = "The following report presents results from: $fromDate to $toDate.";
            $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 .= "The following report presents results from: $reportDate, $fromHour to $toHour.";
        }

        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 .= "The following report presents results from: $fromMinute to $toMinute.";
        }

        return new Content(
            view: 'mail.production_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 [];
        // return [
        //     Attachment::fromData(
        //         fn () => Excel::raw(
        //             new ProductionExport(
        //                 $this->excelData,
        //                 $this->dates
        //             ),
        //             \Maatwebsite\Excel\Excel::XLSX
        //         ),
        //         'production_plan_data.xlsx'
        //     )->withMime(
        //         'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        //     ),
        // ];
    }
}
