<?php

namespace App\Classes\General\Eloquent\Filters;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Log;

class DateRange implements Filter
{
    /**
     * Apply the filter to the query.
     *
     * @param  Builder  $builder
     * @param  mixed  $value
     * @return Builder
     */
    public static function apply(Builder $builder, $value)
    {
        $dt1 = $value[0];
        $dt2 = $value[1];

        $startDate = isset($dt1) && $dt1
            ? Carbon::createFromFormat('Y-m-d', $dt1)->startOfDay()
            : Carbon::now()->subMonth()->startOfDay();

        $endDate = isset($dt2) && $dt2
            ? Carbon::createFromFormat('Y-m-d', $dt2)->endOfDay()
            : Carbon::now()->endOfDay();

        $minAllowedDate = Carbon::createFromFormat('Y-m-d', '2025-07-01')->startOfDay();
        if ($startDate->lt($minAllowedDate) || $endDate->lt($minAllowedDate)) {
            $startDate = Carbon::createFromFormat('Y-m-d', '1970-01-01')->startOfDay();
            $endDate = Carbon::createFromFormat('Y-m-d', '1970-01-01')->startOfDay();
        }

        // return $builder->with([
        //     'company',
        //     'transactions.transactionDetails',
        //     'transactions.voucherRedemption',
        // ])
        // ->whereHas('transactions', function ($query) use ($startDate, $endDate){
        //     $query->payments()
        //     ->complete()
        //     ->whereBetween('created_at', [$startDate, $endDate])
        //     ->latest('created_at');
        // })
        // ->whereDoesntHave('transactions', function ($query) use ($startDate, $endDate) {
        //     $query->payments()
        //     ->complete()
        //     ->where(function ($q) use ($startDate, $endDate) {
        //         $q->where('created_at', '<', $startDate)
        //             ->orWhere('created_at', '>', $endDate);
        //     });
        // });

        return $builder->whereIn('status', [
            ApprovalStatus::APPROVED,
            ApprovalStatus::COMPLETED,
            ApprovalStatus::SUSPENDED,
        ])
        ->where(function ($q) use ($startDate, $endDate) {
            $q->where(function ($q) {
                $q->whereDoesntHave('company', function ($query) {
                    $query->where('debtor', 'N/A NO NEED TO IMPORT');
                });
            })
            // Bookings without refunds
            ->where(function ($q1) use ($startDate, $endDate) {
                $q1->where('status', ApprovalStatus::COMPLETED)
                    ->whereHas('transactions', function ($query) use ($startDate, $endDate){
                        $query->payments()
                        ->complete()
                        ->whereBetween('created_at', [$startDate, $endDate]);
                    })
                    ->whereDoesntHave('transactions', function ($query) {
                        $query->payments()
                        ->complete()
                        ->where(function ($q) {
                            $q->where('created_at', '<', '2025-07-01 00:00:00');
                        });
                    });
            })
            // OR bookings with refunds
            ->orWhere(function ($q2) use ($startDate, $endDate) {
                $q2->whereHas('transactions', function ($query) use ($startDate, $endDate) {
                    $query->payments()
                        ->whereBetween('created_at', [$startDate, $endDate])
                        ->whereHas('transactions', function ($refundQuery) {
                            $refundQuery->refunds()
                                ->whereIn('status', [ApprovalStatus::APPROVED]);
                        });
                })
                ->whereDoesntHave('transactions', function ($query) {
                    $query->payments()
                    ->complete()
                    ->where(function ($q) {
                        $q->where('created_at', '<', '2025-07-01 00:00:00');
                    });
                });
            });
        })
        ->with([
            'company',
            'transactions.transactionDetails',
            'transactions.voucherRedemption',
        ]);
    }
}
