<?php
namespace App\Http\Controllers\API\Notification;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\Customer;
use App\Models\Customer_log_model;
use App\Jobs\Send_email_reject_npwp_job;
use OSS\OssClient;
use OSS\Core\OssException;

class Npwp_controller extends Controller
{
    var $bucket;
    var $ossClient;

    private function validate_input($request)
    {
        $array_validate = [
            'kode_customer' => 'required|exists:customer,code',
            'status' => 'required|in:1,2',
            'reason' => 'requiredif:status,2'
        ];
        $messages = ['reason.requiredif' => 'Harap masukan alasan mengapa di tolak'];
        $request->validate($array_validate, $messages);
    }

    public function do_approve(Request $request)
    {
        $accessKeyId = env('ALIYUN_OSS_ACCESS_KEY_ID');
        $accessKeySecret = env('ALIYUN_OSS_ACCESS_KEY_SECRET');
        $endpoint = env('ALIYUN_OSS_ENDPOINT');
        $this->bucket = env('ALIYUN_OSS_BUCKET');

        try {
            $this->ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        } catch (OssException $e) {
            print $e->getMessage();
        }

        $this->validate_input($request);

        $kode_customer = request('kode_customer');
        $status = request('status');
        $reason = request('reason');

        $data_user = Auth::user();

        $data_customer = $this->get_detail_from_kode($kode_customer);
        if($status == '1') {
            $this->approve_customer($data_customer->id, $data_user->id);
            $this->ossClient->deleteObject($this->bucket, env('ALIYUN_OSS_FOLDER') . '/npwp/'.$data_customer->image_npwp_old);
        }
        else {
            $this->reject_customer($data_customer, $data_user->id);
            $this->ossClient->deleteObject($this->bucket, env('ALIYUN_OSS_FOLDER') . '/npwp/'.$data_customer->image_npwp);
            dispatch(new Send_email_reject_npwp_job($data_customer->email, $data_customer->id, $data_customer->name, $data_customer->code, $reason));           
        }

        $return['success'] = '1';
        return response()->json($return, 200);
    }

    private function get_detail_from_kode($kode_customer)
    {
        return Customer::where('code', $kode_customer)->first();
    }

    private function approve_customer($id_customer, $id_user)
    {
        $data = [
            'alamat_npwp' => null,
            'is_npwp_approved' => '1',
            'npwp_old' => null,
            'image_npwp_old' => null,
            'is_npwp_approved_old' => null,
            'updated_at' => now()
        ];
        Customer::where('id', $id_customer)->update($data);

        $array_log = [
            'act' => 'update',
            'id_customer' => $id_customer,
            'content' => json_encode($data),
            'id_users' => $id_user,
            'created_at' => now()
        ];
        Customer_log_model::insertGetId($array_log);
    }

    private function reject_customer($data_customer, $id_user)
    {
        $data = [
            'alamat_npwp' => null,
            'is_npwp_approved' => $data_customer->is_npwp_approved_old,
            'npwp' => $data_customer->npwp_old,
            'image_npwp' => $data_customer->image_npwp_old,
            'npwp_old' => null,
            'image_npwp_old' => null,            
            'is_npwp_approved_old' => null,
            'updated_at' => now()
        ];
        Customer::where('id', $data_customer->id)->update($data);

        $array_log = [
            'act' => 'update',
            'id_customer' => $data_customer->id,
            'content' => json_encode($data),
            'id_users' => $id_user,
            'created_at' => now()
        ];
        Customer_log_model::insertGetId($array_log);
    }
}