<?php 
namespace App\Exports;

use App\Models\IamPrincipal;
use App\Models\RedeemRestaurant;
use App\Models\Subscriptions;
use App\Models\ReferralUsers;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class ReportExports implements FromView
{
    protected $reportType;
    protected $states;
    protected $startDate;
    protected $endDate;
    protected $restaurants;

    public function __construct($reportType, $states, $startDate, $endDate, $restaurants = [])
    {
        $this->reportType = $reportType;
        $this->states = $states;
        $this->startDate = $startDate;
        $this->endDate = $endDate;
        $this->restaurants = $restaurants;
    }

    public function view(): View
    {
        $data = collect();

        if ($this->reportType === 'Total Subscribed') {
            $query = Subscriptions::query();

            if (!empty($this->states)) {
                $query->whereHas('iamPrincipal', function ($q) {
                    $q->whereIn('state_xid', $this->states);
                });
            }

            if ($this->startDate) {
                $query->whereDate('created_at', '>=', $this->startDate);
            }

            if ($this->endDate) {
                $query->whereDate('created_at', '<=', $this->endDate);
            }

            $data = $query->get();
            // dd($data);
        } elseif ($this->reportType === 'Total Users') {
            $query = IamPrincipal::query()->where('principal_type_xid',3);

            if (!empty($this->states)) {
                $query->whereIn('state_xid', $this->states);
            }

            if ($this->startDate) {
                $query->whereDate('created_at', '>=', $this->startDate);
            }

            if ($this->endDate) {
                $query->whereDate('created_at', '<=', $this->endDate);
            }

            $data = $query->get();
        } elseif ($this->reportType === 'Redemptions') {
            $query = RedeemRestaurant::with(['restaurant', 'customer'])->where('is_redeem', 0);

            if (!empty($this->states)) {
                $query->whereHas('customer', function ($q) {
                    $q->whereIn('state_xid', $this->states);
                });
            }

            if ($this->startDate) {
                $query->whereDate('created_at', '>=', $this->startDate);
            }

            if ($this->endDate) {
                $query->whereDate('created_at', '<=', $this->endDate);
            }

            $data = $query->get();
        } elseif ($this->reportType === 'Redemptions for Specific Restaurants') {
            $query = RedeemRestaurant::with('restaurant', 'customer');

            if (!empty($this->restaurants)) {
                $query->whereIn('manage_restaurants_xid', $this->restaurants);
            }

            if ($this->startDate) {
                $query->whereDate('created_at', '>=', $this->startDate);
            }

            if ($this->endDate) {
                $query->whereDate('created_at', '<=', $this->endDate);
            }

            $data = $query->get();
        } elseif ($this->reportType === 'Subscriptions Cancelled') {
            $query = Subscriptions::query()->where('is_cancelled_subscription', 1);

            if (!empty($this->states)) {
                $query->whereHas('iamPrincipal', function ($q) {
                    $q->whereIn('state_xid', $this->states);
                });
            }

            if ($this->startDate) {
                $query->whereDate('created_at', '>=', $this->startDate);
            }

            if ($this->endDate) {
                $query->whereDate('created_at', '<=', $this->endDate);
            }

            $data = $query->get();
        }

        elseif ($this->reportType === 'Subscriptions Cancelled') {
            $query = Subscriptions::with('iamPrincipal')->where('is_cancelled_subscription', 1);

            if (!empty($this->states)) {
                $query->whereHas('iamPrincipal', function ($q) {
                    $q->whereIn('state_xid', $this->states);
                });
            }

            if ($this->startDate) {
                $query->whereDate('created_at', '>=', $this->startDate);
            }

            if ($this->endDate) {
                $query->whereDate('created_at', '<=', $this->endDate);
            }

            $data = $query->get();
        }

        elseif ($this->reportType === 'Referrals Made') {
            $query = ReferralUsers::query()
                ->with(['referredUser', 'referredUser.state'])  
                ->whereHas('referredUser', function ($query) {
                    $query->whereIn('state_xid', $this->states);
                });

            if (!empty($this->startDate)) {
                $query->whereDate('referred_date_time', '>=', $this->startDate);
            }

            if (!empty($this->endDate)) {
                $query->whereDate('referred_date_time', '<=', $this->endDate);
            }

            $data = $query->get();
        }

        elseif ($this->reportType === 'Referees Joined') {
            $query = ReferralUsers::query()
                ->with(['refeersUser', 'refeersUser.state'])
                ->whereHas('refeersUser', function ($query) {
                    $query->whereIn('state_xid', $this->states);
                });

            if (!empty($this->startDate)) {
                $query->whereDate('referred_date_time', '>=', $this->startDate);
            }

            if (!empty($this->endDate)) {
                $query->whereDate('referred_date_time', '<=', $this->endDate);
            }

            $data = $query->get();
        }
        
        return view('exports.report', [
            'data' => $data,
            'reportType' => $this->reportType
        ]);
    }
}
