<?php

namespace App\Console\Commands;

use App\Models\Client;
use Illuminate\Console\Command;

class TestCriteriaQueries extends Command
{



    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'criteria:test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test criteria queries';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    
    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
       
        $testSteps = [
            'MCB Primary? YES',        
            [
                'criteriaName'=>'MCP',
                'arguments'=>[
                    'pro_id'=>'d88b20df-4c2e-430a-a7a1-7d08fbbc1c82'
                ]
            ]
        ];

        $query = Client::whereRaw('id > :id', ['id' => 0]);

        $query = $this->queryBuilder($query, $testSteps);

        $this->info("The count is: ".$query->count());

        $records = $query->get();
        foreach($records as $record){
            $this->info($record->uid);
        }
    }

   

    function queryBuilder($query, $testCriteria){

        foreach($testCriteria as $criterion){

            $clause = '';
            $params = [];

            if(is_string($criterion)){
                // $clause = $this->criteria[$criterion]['clause'];
                // $params = $this->criteria[$criterion]['arguments'];
                $query = $query->whereRaw($this->criteria[$criterion]);
            }
            if(is_array($criterion)){
                $criteriaName = $criterion['criteriaName'];
                $clause = $this->criteria[$criteriaName]['clause'];
                $params = $criterion['arguments'];  
                $query = $query->whereRaw($clause, $params);           
            }

          
        }
    
        return $query;
    }
    
    protected $criteria = [

        //with preset arguments
        'Client active? YES' => "()",
        'Client active? NO' => "()",
        'MCP assigned? YES' => "()",
        'MCP assigned? NO' => "()",
        'Future MCP appointment? YES' => "()",
        'Future MCP appointment? NO' => "()",

        'MCB Primary? YES' => "(is_part_b_primary = 'YES')",

        'MCB Primary? NO' => "()",
        'Has active RM device? YES' => "()",
        'Has active RM device? NO' => "()",
        'Active device type? Weight' => "()",
        'Active device type? BP' => "()",
        'Active device type? Weight ONLY' => "()",
        'Active device type? BP ONLY' => "()",
        'Active device type? Weight + BP' => "()",
        'Type 2 Diabetic' => "(client.id IN (SELECT client_id FROM dx))",

        //with user defined arguments
        'MCP' => [
            'argument_ui_map' => [
                'pro_uid' => [
                    'field_type' => 'record',
                    'table' => 'pro',
                    'display' => '{name_display} - {npi}',
                    'value' => 'uid' 
                ]
            ],
            'arguments' => ['pro_uid'],
            'clause' => "(client.mcp_pro_id = (SELECT pro.id FROM pro WHERE pro.uid = :pro_uid))"
        ],
        'Chart Creation Date Between' => [
            'argument_ui_map' => [
                'starting_date' => [
                    'field_type' => 'date'
                ],
                'ending_date' => [
                    'field_type' => 'date'
                ]
            ],
            'arguments' => ['starting_date', 'ending_date'],
            'clause' => "(client.created_at::DATE >= :starting_date AND client.created_at::DATE =< :ending_date)"
        ]
    ];
}
