<?php
/**
 * Created by PhpStorm.
 * User: software
 * Date: 10/20/17
 * Time: 2:32 PM
 */

namespace App\Http\Controllers\Ads;


use App\Http\Controllers\Controller;
use App\Tools\TMNTools;

// centralized ads system:
// contact damian [adamian@npontu.com] for explanation if any. thanks
class ITargetController extends Controller
{
    private $tool;

    public function __construct()
    {
        $this->tool = new TMNTools();
    }
    
    // fetch target | demographics
    public function index()
    {
        $targets = $this->tool->nodes();
        return response()->json($targets);
    }

    // filter
    public function filter()
    {
        $_req   = request()->except(['_token']);

        // handle city
        $city   = isset($_req['city']) ? $_req['city'] : null;
        $_req['city'] = $city != null ? explode(',', $city) : [];

        $fil    = $this->tool->filterize($_req);
        $mode   = array_key_exists('mode', $_req) ? $_req['mode'] : 'MATCH_ANY';
        $data   = $this->tool->people($fil, $mode);
        return response()->json($data);
    }
}