<?php

namespace App\Services\Dashboard\Order;

use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Http\Request;

class OrderFilterService
{
    protected $request, $id, $client_phone, $status, $payment_status, $payment_type, $client_type, $delivery_type, $from, $to, $with_didox, $with_installation;

    public function __construct(Request $request)
    {
        $this->id = empty($request->get('id')) ? null : $request->get('id');
        $this->client_phone = empty($request->get('client_phone')) ? null : $request->get('client_phone');
        $this->status = empty($request->get('order_status')) ? null : $request->get('order_status');
        $this->payment_status = empty($request->get('payment_status')) ? null : $request->get('payment_status');
        $this->payment_type = empty($request->get('payment_type')) ? null : $request->get('payment_type');
        $this->client_type = empty($request->get('client_type')) ? null : $request->get('client_type');
        $this->delivery_type = empty($request->get('delivery_type')) ? null : $request->get('delivery_type');
        $this->with_didox = $request->get('with_didox');
        $this->with_installation = $request->get('with_installation');
        $this->from = empty($request->get('from')) ? Carbon::parse('2000-01-01')->format('Y-m-d') : Carbon::parse($request->get('from'))->format('Y-m-d');
        $this->to = empty($request->get('to')) ? Carbon::parse('2040-01-01')->format('Y-m-d') : Carbon::parse($request->get('to'))->format('Y-m-d');
    }

    public function filter()
    {
        $orders = Order::query()
            ->when($this->id, function ($query) {
                return $query->where('id', $this->id);
            })
            ->when($this->client_phone, function ($query) {
                return $query->where('phone', 'like', '%' . $this->formatPhone($this->client_phone) . '%');
            })
            ->when($this->status, function ($query) {
                return $query->where('status', $this->status);
            })
            ->when($this->payment_status, function ($query) {
                return $query->where('payment_status', $this->payment_status);
            })
            ->when($this->payment_type, function ($query) {
                return $query->where('payment_type', $this->payment_type);
            })
            ->when($this->client_type, function ($query) {
                return $query->where('client_type', $this->client_type);
            })
            ->when($this->delivery_type, function ($query) {
                return $query->where('delivery_type', $this->delivery_type);
            })
            ->when($this->with_didox, function ($query) {
                if (in_array('unchecked', $this->with_didox)) {
                    $query->orWhere('with_didox', 0);
                }
                if (in_array('checked', $this->with_didox)) {
                    $query->orWhere('with_didox', 1);
                }
                return $query;
            })
            ->when($this->with_installation, function ($query) {
                if (in_array('unchecked', $this->with_installation)) {
                    $query->orWhere('with_installation', 0);
                }
                if (in_array('checked', $this->with_installation)) {
                    $query->orWhere('with_installation', 1);
                }
                return $query;
            })
            ->whereBetween('created_at', [$this->from, $this->to])
            ->paginate(20);

        return $orders;
    }

    private function formatPhone($phone)
    {
        return preg_replace('/[^0-9]/', '', $phone);
    }
}
