<?php

namespace App\ModelFilters;

use Carbon\Carbon;
use EloquentFilter\ModelFilter;

class PlanFilter extends ModelFilter
{
    /**
    * Related Models that have ModelFilters as well as the method on the ModelFilter
    * As [relationMethod => [input_key1, input_key2]].
    *
    * @var array
    */
    public $relations = [];
    public function apply($query)
    {
        return $query;
    }

    public function assetId($assetId)
    {
        return $this->where('asset_id', $assetId);
    }

    public function expired($expired): PlanFilter
    {
        if (! in_array($expired, ['yes', 'no'])) {
            return $this;
        }

        return $this->when($expired == "yes", function ($query) {
            return $query->where('end', "<=", Carbon::now()->toDateString());
        })->when($expired == "no", function ($query) {
            return $query->where('end', ">", Carbon::now()->toDateString())->orWhereNull('end');
        });
    }
}