<?php

namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Validator;
use App\CustomerSource;
use App\Customer;
use App\Http\Requests\CustomerSourceRequest;

class CustomerSourceController extends Controller
{

    public function index()
    {

		$sources = CustomerSource::where('company_id', $this->_getCompany()->id)
										->orderBy('name', 'asc')
											->get();

		return response()->json($sources, 200);
	
    }

    public function store(CustomerSourceRequest $request)
    {
		
		$source = $this->_save($request);
		
		if ($source) {
			return response()->json([
				'title' => 'Customer Source',
				'message' => 'Customer Source was added successfully.',
				'object' => $source
			], 201);
		} else {
			return response()->json([
				'title' => 'Customer Source',
				'message' => 'It was not possible to add the Customer Source.'
			], 500);
		}
	
    }

    public function show($id)
    {

		$source = CustomerSource::find($id);

		if (!$source || $source->company_id != $this->_getCompany()->id) {
			return response()->json([
				'title' => 'Customer Source',
				'message' => 'Record not found'
			], 404);
		}

		return response()->json($source, 200);
	
    }

    public function update(CustomerSourceRequest $request, $id)
    {

		$source = $this->_save($request, $id);

		if ($source) {
			return response()->json([
				'title' => 'Customer Source',
				'message' => 'Customer Source was edited successfully.',
				'object' => $source
			], 200);
		} else {
			return response()->json([
				'title' => 'Customer Source',
				'message' => 'It was not possible to edit the Customer Source.'
			], 500);
		}
	
    }

    public function destroy($id)
    {

		$source = CustomerSource::find($id);

        if (!$source || $source->company_id != $this->_getCompany()->id) {
            return response()->json([
				'title' => 'Customer Source',
                'message' => 'Record not found',
            ], 404);
        }

        $source->delete();
		
		return response()->json([
				'title' => 'Customer Source',
				'message' => 'Customer Source was deleted successfully.'
			], 200);
	
    }
	
	private function _save(Request $request, $id = null) 
    {

		if ($id) {

			$source = CustomerSource::find($id);

			if (!$source || $source->company_id != $this->_getCompany()->id) {
				return response()->json([
					'title' => 'Customer Source',
					'message' => 'Record not found',
				], 404);
			}

		} else {

			$source = new CustomerSource();
			
			$source->created_at = date('Y-m-d H:i:s');

		}
		
		$source->updated_at = date('Y-m-d H:i:s');
		$source->company_id = $this->_getCompany()->id;
		$source->name = $request->name;

		if ($source->save()) {
			return $source;
		}

		return null;

    }
	
	public function customers($id)
	{
		
		$source = CustomerSource::find($id);

		if (!$source || $source->company_id != $this->_getCompany()->id) {
			return response()->json([
				'title' => 'Customer Source',
				'message' => 'Record not found',
			], 404);
		}
		
		return response()->json($source->customers, 200);
		
	}
	
	public function reports(Request $request)
	{
		
		$sources = CustomerSource::where('company_id', $this->_getCompany()->id)
										->orderBy('name', 'asc')
											->get();
		
		$report = array();
		
		foreach ($sources as $source) {
			
			$query = $source->customers;
			
			if ($request->date_from) {
				$query = $query->where('created_at', '>=', $request->date_from . ' 00:00:00');
			}
			
			if ($request->date_to) {
				$query = $query->where('created_at', '<=', $request->date_to . ' 23:59:59');
			}
			
			$report[] = array(
				'label' => $source->name . ' (' . $query->count() . ')',
				'name' => $source->name,
				'data' => $query->count()
			);
		
		}

		return response()->json($report, 200);
		
	}
	
}
