<?php

namespace App\Http\Modules\User;

use Auth;
use App\Models\ContractObligationDependency;
use App\Models\ContractObligation;

use App\Http\Modules\User\ContractObligationModule;

use App\Http\Rules\User\UnderCheck;
use App\Http\Rules\User\ExistCheck;

use App\Http\Helpers\General;
use App\Http\Helpers\Hasher;
use Carbon\Carbon;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;

use App\Http\Controllers\User\BaseController;

class ContractObligationDependencyModule extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    public static function list(Request $request)
    {
    	General::storeCachePerviousQuery($request->query(), 'contract_query');
     
        $name = $request->get('name', null);
        $status = $request->get('status', []);
        $page = $request->get('page', null);
        $limit = $request->get('limit', 10);

        $query = ContractObligationDependency::query();

         if (strlen($name) > 0) {
            $query->where('name', 'like', '%' . $name . '%');
        }

        if (!empty($status) > 0) {
            $query->whereIn('status', $status);
        }

        $query->where('user_id', Auth::guard('user-api')->user()->id);

        $query->orderBy('id', 'desc');

        $params = [
            'name' => $name,
            'status' => $status,
            'page' => $page
        ];

        $contract_list = $query->paginate($limit);

        $extract = [];
        foreach ($contract_list as $key => $row) {
            $extract[] = $row;

        }
        $pagination = General::paginationGenerator($contract_list, $params, $extract);

        return $pagination;
    }

    public static function show(Request $request, $hash_id)
    {
        $id = Hasher::decode('contracts', $hash_id);
        $user_id = Auth::guard('user-api')->user()->id;
        $contract = Contract::
            where('id', $id)
            ->where('user_id', $user_id)
            ->first();
        if ($contract) {
            $data = General::returnData($contract, '');
            return response()->json($data);
        }
        $data = General::returnNotFoundData();
        return response()->json($data);
    }

    public static function generate(Request $request)
    {
        $validation = ContractObligationDependencyModule::validation($request);
        if (!$validation->status) {
            return response()->json($validation, 422);
        }

        DB::beginTransaction();

        $contract_obligation_id = $request->input('contract_obligation_id');
        $contract_obligation_id = Hasher::decode('contract_obligations', $contract_obligation_id[0]);
        $contract_obligation = ContractObligation::find($contract_obligation_id);

        $depend_contract_obligation_id = $request->input('depend_contract_obligation_id');
        $depend_contract_obligation_id = Hasher::decode('contract_obligations', $depend_contract_obligation_id[0]);
        $depend_contract_obligation = ContractObligation::find($depend_contract_obligation_id);

        $contract_obligation_dependency = new ContractObligationDependency();
        $contract_obligation_dependency->user_id = Auth::guard('user-api')->user()->id;
        $contract_obligation_dependency->contract_obligation_id = $contract_obligation->id;
        $contract_obligation_dependency->depend_contract_obligation_id = $depend_contract_obligation->id;
        $contract_obligation_dependency->status = config('constants.contract_dependency.status.active');
        $contract_obligation_dependency->save();

        // log
        ContractObligationModule::makeLog($contract_obligation->id, 'user (' . Auth::guard('user-api')->user()->id . ') created dependency');

        DB::commit();

        $query_string = General::getCachePerviousQuery('contract_query');
        $data = General::returnData($contract_obligation, $query_string);

        return response()->json($data);
    }

    private static function validation(Request $request, $id = '', $method = 'POST')
    {
        $data = $request->all();
        $rule= [];

        if ($method == 'POST') {
           
        }

        if ($method == 'PUT') {
           
        }

        if ($method == 'DEL') {
            $data['id'] = $id;
            $rule = [
                'id' => [new UnderCheck('contract'), new ExistCheck('contract')],
            ];
        }

        $validator = Validator::make($data, $rule, config('error_code'));

        $errors = $validator->errors();

        if ($validator->fails()) {
            $data = General::returnErrorData($errors);
            return $data;
        }
        else {
            return (object)['status' => true];
        }
    }

    public static function makeLog($id = '', $remark = '') 
    {
        $contract_obligation = ContractObligation::find($id);
        $contract_obligation_log = new ContractObligationLog();
        $contract_obligation_log->contract_obligation_id = $contract_obligation->id;
        $contract_obligation_log->history = json_encode($contract_obligation);
        $contract_obligation_log->remark = $remark;
        $contract_obligation_log->save();
    }
}
