<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Mail\MailTicket;
use App\Models\Sales\Invoice;
use App\Models\Facility\Layout;
use App\Models\Facility\Seats;
use App\Models\Sales\Ticket;
use App\Models\Sales\TicketSeat;
use App\Models\Settings\ScheduleLog;
use Mail;
use App\Mail\TicketMail;

class MailCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mail:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command will mail tickets';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $todaytime = date("Y-m-d H:i:s");
        $insertobj_scheduler = ScheduleLog::create([
            'command' => 'mail:cron',
            'command_log' => "mail will check mail tickets scheduler started at ".$todaytime,
            'starttime' => $todaytime,
            'status' => 0,
        ]);
        $scheudleid = $insertobj_scheduler->id;
        //return 0;
        $mail_status = "";
        //Mailing tickets with status 0
        $mails = MailTicket::where('status',0)->where('mail_type',1)->get();
        //dd($mails);
        foreach($mails as $mail)
        {
            $email = $mail->email;
            $invoice_id = $mail->invoice_id;
            $mailid = $mail->id;  //// primary id of mail table
            $invoicemd5 = md5($invoice_id);
            $mail_status = 2;
            $mailobj_before = MailTicket::where('id',$mailid)->first();
            $mailobj_before->status=$mail_status;
            $mailobj_before->save();
            ///check whether the invoice is cancelled.
            $invoice_check = Invoice::where('id',$invoice_id)->first();
            if($invoice_check->status!=1)
                continue;
            ///////////////////////////////////////
            //generate ticket pdf
            $invoice = Invoice::where('id',$invoice_id)->where('status',1)->with('organisation','tickets')->first();
            if(isset($invoice))
            {
                $mail_status=1;
                $seatid =[];
                $seatname_ticket = [];
                $seat_count=0;
                $seat_name_row= [];
                $totalrows = "";
                $tickets = Ticket::where('invoice_id',$invoice_id)->where('status',1)->with('showtime','facility','invoice')->get();
                foreach($tickets as $ticket)
                {
                    if($ticket->facility->fixedseatflag)
                    {
                        $ticket_id = $ticket->id;
                        $facility_id = $ticket->facility_id;
                        $layouts[$facility_id] = Layout::where('facility_id', $facility_id)->first();
                        if(isset($layouts[$facility_id]))
                        {
                            $totalrows = $layouts[$facility_id]->total_row;
                            $layout_id = $layouts[$facility_id]->id;
                            //get seats
                            for($i=1;$i<=$totalrows;$i++)
                            {
                                $seats[$facility_id][$i] = Seats::where('facility_id',$facility_id)->where('layout_id',$layout_id)->where('row_no',$i)->get();
                                foreach($seats[$facility_id][$i] as $seatobj)
                                {
                                    $seat_name_row[$i][] = $seatobj->seat_name;
                                }
                            }
                        }
                        $ticket_seats = TicketSeat::where('ticket_id',$ticket_id)->where('status',1)->with('seat')->get();
                        foreach($ticket_seats as $ticket_seat)
                        {
                            $seat_count++;
                            $seatid[] = $ticket_seat->seat_id;
                            $seatname_ticket[] = $ticket_seat->seat->seat_name;
                        }
                    }
                }
                $invoice_pdf = \PDF::loadView('tickets.invoicepdf', compact('invoice','totalrows','seat_name_row','seatname_ticket'));
                \Storage::put('temp/'.$invoicemd5.'.pdf', $invoice_pdf->output());
                $ticketname = $invoicemd5.'.pdf';
                $path = public_path()."/temp/".$ticketname;
                if($email)
                {
                    $data= ['message' => 'Congrats, You have booked the tickets. Please find ticket in attachment!', 'mailto'=>$email, 'ticketname'=>$ticketname, 'attached_data'=>$path ];
                    //mailing to the registered email
                    Mail::to($email)->send(new TicketMail($data));
                }
                //After mailing make the status 1
                $mailid = $mail->id;
                $mailobj = MailTicket::where('id',$mailid)->first();
                $mailobj->status=$mail_status;
                $mailobj->save();

                unlink($path);
            }
            else
            {
                $mail_status =2;
                $mailobj = MailTicket::where('id',$mailid)->first();
                $mailobj->status=$mail_status;
                $mailobj->save();
            }

            
           
        }


        $todaytimeend = date("Y-m-d H:i:s");
        $scheudlerlogupd = ScheduleLog::where('id',$scheudleid)->where('status',0)->first();
        if(isset($scheudlerlogupd))
        {
            $scheudlerlogupd->status = 1;
            $scheudlerlogupd->endtime = $todaytimeend;
            $scheudlerlogupd->save();
        } 
        
    }
}
