<?php

namespace app\Helpers;
use App\Models\Sales\Ticket;
use App\Models\Facility\Facility;
use App\Models\Sales\Invoice;
use App\Models\Sales\TicketSeat;
use App\Models\Settings\Configuration;
use App\Models\Sales\ShowCancel;
use PDF;
use Mail;

class BookingClass {

    function __construct() {

    }

    

    public function getAvailableSeats($facility_id, $show_id, $show_date){
       //get available seats of a show
        $totaladultcount = Ticket::where('show_date',$show_date)->where('facility_id',$facility_id)->where('show_time_id',$show_id)->where('status',1)->sum('noof_adult');
        $totalchildcount = Ticket::where('show_date',$show_date)->where('facility_id',$facility_id)->where('show_time_id',$show_id)->where('status',1)->sum('noof_child');

        $totalusedseats = $totaladultcount + $totalchildcount;

        $facility = Facility::where('id',$facility_id)->where('active_flag',1)->first();
        $totalseats = $facility->noofseats;

        $seatsavailable = $totalseats - $totalusedseats;

        // blocked seats
        $time = time();
        $configuration = Configuration::where('id',1)->first();
        $ticket_time_out = $configuration->ticket_time_out+1;
        $time = $time - ($ticket_time_out*60);
        $checktime = date("Y-m-d H:i:s", $time);
        $totaladultcount = Ticket::where('show_date',$show_date)->where('facility_id',$facility_id)->where('show_time_id',$show_id)->where('status',0)->where('created_at','>=',$checktime)->sum('noof_adult');
        $totalchildcount = Ticket::where('show_date',$show_date)->where('facility_id',$facility_id)->where('show_time_id',$show_id)->where('status',0)->where('created_at','>=',$checktime)->sum('noof_child');
        $totalblockedseats = $totaladultcount + $totalchildcount;
        $seatsavailable = $seatsavailable - $totalblockedseats;


        //todo check cancel show to check if show is cancelled




        return $seatsavailable;


    }

    public function getBookedSeats($facility_id, $show_id, $show_date)
    {
      $seats = [];
      //get booked seats from ticket seat of facility 
      $ticketseat = TicketSeat::where('show_time_id',$show_id)->where('status',1)->whereHas('ticket', function($q) use($facility_id, $show_date){
              $q->where('facility_id',$facility_id)->where('show_date',$show_date);
          })->get();


      foreach($ticketseat as $seat)
      {

         $seats[] = $seat->seat_id;
      }

      //get blocked seat
      $time = time();
      $configuration = Configuration::where('id',1)->first();
      $ticket_time_out = $configuration->ticket_time_out+1;
      $time = $time - ($ticket_time_out*60);
      $checktime = date("Y-m-d H:i:s", $time);
      $ticketseat = TicketSeat::where('show_time_id',$show_id)->where('status',0)->whereHas('ticket', function($q) use($facility_id, $show_date, $checktime){
              $q->where('facility_id',$facility_id)->where('show_date',$show_date)->where('created_at','>=',$checktime);
          })->get();
      foreach($ticketseat as $seat)
      {

         $seats[] = $seat->seat_id;
      }

      return $seats;

    }

    public function getnewInvoice($organisation_id){
      //get min date and max date
      $year = $this->getCurrentYear();
      $firstday = $this->getfirstDate($year);
      $lastday = $this->getlastDate($year);

      $maxinvoiceno = Invoice::where('organisation_id',$organisation_id)->where('invoice_date','>=',$firstday)->where('invoice_date','<=',$lastday)->max('invoice_no');

      $invoicearr=[];
      $invoicearr[0] = $maxinvoiceno+1;
      $invoicearr[1] = date('Y-m-d H:i:s');
      return $invoicearr;
    }
    public function getnewTicket($facility_id){
      //get min date and max date
      $year = $this->getCurrentYear();
      $firstday = $this->getfirstDate($year);
      $lastday = $this->getlastDate($year);

      $maxticketno = Ticket::where('facility_id',$facility_id)->where('show_date','>=',$firstday)->where('show_date','<=',$lastday)->max('ticket_no');

      return $maxticketno+1;

    }

    public function getCurrentYear()
    {
      if ( date('m') >= 4 ) {
          $year = date('Y');
      }
      else {
          $year = date('Y')-1;
      }
      return $year;
    }

    public function getfirstDate($year)
    {
      return $year."-04-01";
    }
    public function getlastDate($year)
    {
      $nextyear = $year+1;
      return $nextyear."-03-31";
    }

    public function mailTicket($email,$invoice_id)
    {
      //mail invoice to the email specified

      //generate invoice_pdf
      $invoice = Invoice::where('id',$invoice_id)->where('status',1)->with('organisation','tickets')->first();
      $invoice_pdf = PDF::loadView('tickets.invoicepdf', compact('invoice'));
      return $invoice_pdf->output();

      


    }
}
?>