<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Responses;
use App\Helper;
use App\Models\Users;
use App\Models\Clients;
use App\Models\Insurances;

class InsuranceController extends Controller
{
    public function view_insurances(Request $req)
    {
        return view('menu_v1.configs.insurances');
    }

    /**
	 * API
	 */

	public function api_list_insurances(Request $req)
	{
		try {
			$now = time();
			$input = [];
			$rulesInput = [];

			// validasi input
			// $isValidInput = Validator::make($input, $rulesInput);
			// if (!$isValidInput->passes()) {
			// 	$apiResp = Responses::bad_input($isValidInput->messages()->first());
			// 	return new Response($apiResp, $apiResp['meta']['code']);
			// }

			$list = Insurances::listInsurances();
			foreach ($list as $key => $row) {
				$list[$key]->DT_RowIndex = $key + 1;
				$list[$key]->action = '-';
			}

			$apiResp = Responses::success('success list insurances');
			$apiResp['count'] = count($list);
			$apiResp['data'] = $list;
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_show_insurance(Request $req, $iid)
	{
		try {
			$now = time();

			$input = [
				'iid' => $iid,
			];
			$rulesInput = [
				'iid' => 'required|integer|not_in:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$insurance = Insurances::showInsuranceById($iid);
			if (count($insurance) < 1) {
				$apiResp = Responses::not_found('insurance not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}
			
			$apiResp = Responses::success('success get detail insurance');
			$apiResp['data'] = $insurance[0];
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_add_insurance(Request $req)
	{
		try {
			$now = time();

			$input = [
				'name' => $req->name,
				'desc' => $req->desc,
				'price' => $req->price,
				'min_price' => $req->min_price,
				'max_price' => $req->max_price,
				'status' => $req->status,
			];
			$rulesInput = [
				'name' => 'required|string',
				'desc' => 'nullable|string',
				'price' => 'required|numeric|min:0',
				'min_price' => 'required|numeric|min:0',
				'max_price' => 'required|numeric|min:0',
				'status' => 'required|numeric|min:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			DB::beginTransaction();

			$insInsurance = [
				'premi_name' => $req->name,
				'desc' => $req->desc,
				'premi_price' => $req->price,
				'premi_min_price' => $req->min_price,
				'premi_max_price' => $req->max_price,
				'is_active' => $req->status,
				'crt' => $now,
				'crt_by' => $req->auth->uid,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			$iid = Insurances::addInsurance($insInsurance);

			$apiResp = Responses::created('success add new insurance');

			DB::commit();
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_edit_insurance(Request $req, $iid)
	{
		try {
			$now = time();

			$input = [
				'iid' => $iid,
				'name' => $req->name,
				'desc' => $req->desc,
				'price' => $req->price,
				'min_price' => $req->min_price,
				'max_price' => $req->max_price,
				'status' => $req->status,
			];
			$rulesInput = [
				'iid' => 'required|integer|not_in:0',
				'name' => 'required|string',
				'desc' => 'nullable|string',
				'price' => 'required|numeric|min:0',
				'min_price' => 'required|numeric|min:0',
				'max_price' => 'required|numeric|min:0',
				'status' => 'required|numeric|min:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$insurance = Insurances::showInsuranceById($iid);
			if (count($insurance) < 1) {
				$apiResp = Responses::not_found('insurance not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			$updtInsurance = [
				'premi_name' => $req->name,
				'desc' => $req->desc,
				'premi_price' => $req->price,
				'premi_min_price' => $req->min_price,
				'premi_max_price' => $req->max_price,
				'is_active' => $req->status,
				'updt' => $now,
				'updt_by' => $req->auth->uid,
			];
			if ($req->dest_district) {
				$updtInsurance['dest_district'] = $req->dest_district;
			}
			$iid = Insurances::updateInsurance($iid, $updtInsurance);

			$apiResp = Responses::created('success update insurance');

			DB::commit();
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_del_insurance(Request $req, $iid)
	{
		try {
			$now = time();

			$input = [
				'iid' => $iid,
			];
			$rulesInput = [
				'iid' => 'required|integer|not_in:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$insurance = Insurances::showInsuranceById($iid);
			if (count($insurance) < 1) {
				$apiResp = Responses::not_found('insurance not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			Insurances::updateInsurance($iid, [
				'dlt' => $now,
				'dlt_by' => $req->auth->uid,
			]);

			DB::commit();
			
			$apiResp = Responses::success('success delete insurance');
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}
}
