<?php

class SertifikasiHalalController extends BaseController
{
    public function getProvince()
    {
        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop_1']);
        $client->setLogger($this->logger);

        try {
            $result = $client->province();
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse($result);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Provinsi tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses mendapatkan provinsi gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function getCity()
    {
        $getParams = $this->request->get();

        $getParams['kodeprovinsi'] = array_key_exists('kodeprovinsi', $getParams) ? $getParams['kodeprovinsi'] : null;

        $params = $this->validate(
            $getParams,
            [
                'kodeprovinsi' => ['validators' => []]
            ]
        );

        // if (empty($params['kodeprovinsi'])) {
        //     throw new CustomErrorException(BaseResponse::INVALID_REQUEST, 'Permintaan tidak valid');
        // }

        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop_1']);
        $client->setLogger($this->logger);

        try {
            $result = $client->city($params);
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse($result);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Kota Kabupaten tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses mendapatkan kota kabupaten gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function getService()
    {
        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop_1']);
        $client->setLogger($this->logger);

        try {
            $result = $client->service();
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse($result['data']);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Jenis layanan tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses mendapatkan jenis layanan gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function getProductType()
    {
        $getParams = $this->request->get();

        $getParams['kodelayanan'] = array_key_exists('kodelayanan', $getParams) ? $getParams['kodelayanan'] : null;

        $params = $this->validate(
            $getParams,
            [
                'kodelayanan' => ['validators' => []]
            ]
        );

        // if (empty($params['kodelayanan'])) {
        //     throw new CustomErrorException(BaseResponse::INVALID_REQUEST, 'Permintaan tidak valid');
        // }

        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop_1']);
        $client->setLogger($this->logger);

        try {
            $result = $client->product_type($params);
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse(!$result['data'] ? [] : $result['data']);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Jenis produk tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses mendapatkan jenis produk gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function getLph()
    {
        $getParams = $this->request->get();

        $getParams['kode_provinsi'] = array_key_exists('kode_provinsi', $getParams) ? $getParams['kode_provinsi'] : null;

        $params = $this->validate(
            $getParams,
            [
                'kode_provinsi' => ['validators' => []]
            ]
        );

        // if (empty($params['kode_provinsi'])) {
        //     throw new CustomErrorException(BaseResponse::INVALID_REQUEST, 'Permintaan tidak valid');
        // }

        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop_1']);
        $client->setLogger($this->logger);

        try {
            $result = $client->lph($params);
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse(!$result['data'] ? [] : $result['data']);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "LPH tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses mendapatkan LPH gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function costCalculating()
    {
        $rawBody = $this->request->getJsonRawBody(true);

        // $getParams['jenis_layanan'] = array_key_exists('jenis_layanan', $getParams) ? $getParams['jenis_layanan'] : null;
        // $getParams['jenis_produk'] = array_key_exists('jenis_produk', $getParams) ? $getParams['jenis_produk'] : null;
        // $getParams['jumlah_pabrik'] = array_key_exists('jumlah_pabrik', $getParams) ? $getParams['jumlah_pabrik'] : null;
        // $getParams['jumlah_produk'] = array_key_exists('jumlah_produk', $getParams) ? $getParams['jumlah_produk'] : null;
        // $getParams['kabupaten_id'] = array_key_exists('kabupaten_id', $getParams) ? $getParams['kabupaten_id'] : null;
        // $getParams['lph_id'] = array_key_exists('lph_id', $getParams) ? $getParams['lph_id'] : null;
        // $getParams['provinsi_id'] = array_key_exists('provinsi_id', $getParams) ? $getParams['provinsi_id'] : null;
        // $getParams['skala_usaha'] = array_key_exists('skala_usaha', $getParams) ? $getParams['skala_usaha'] : 'JU.3';

        $params = $this->validate(
            $rawBody,
            [
                'jenis_layanan' => ['validators' => ['required']],
                'jenis_produk' => ['validators' => ['required']],
                'jumlah_pabrik' => ['validators' => ['required', 'numeric']],
                'jumlah_produk' => ['validators' => ['required', 'numeric']],
                'kabupaten_id' => ['validators' => ['required']],
                'lph_id' => ['validators' => ['required']],
                'provinsi_id' => ['validators' => ['required']],
                'skala_usaha' => ['validators' => ['required']],
            ]
        );

        if (
            empty($params['jenis_layanan']) ||
            empty($params['jenis_produk']) ||
            empty($params['jumlah_pabrik']) ||
            empty($params['jumlah_produk']) ||
            empty($params['kabupaten_id']) ||
            empty($params['lph_id']) ||
            empty($params['provinsi_id']) ||
            empty($params['skala_usaha']) ||
            (!empty($params['jumlah_pabrik']) && (int) $params['jumlah_pabrik'] < 0) ||
            (!empty($params['jumlah_produk']) && (int) $params['jumlah_produk'] < 0)
        ) {
            throw new CustomErrorException(BaseResponse::INVALID_REQUEST, 'Permintaan tidak valid');
        }

        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop_1']);
        $client->setLogger($this->logger);

        try {
            $result = $client->calculate($params);
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse($result);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Perhitungan gagal", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses perhitungan gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function searchLocalCertificate()
    {
        $getParams = $this->request->get();

        $getParams['page'] = array_key_exists('page', $getParams) ? $getParams['page'] : 1;
        $getParams['size'] = array_key_exists('size', $getParams) ? $getParams['size'] : 10;
        $getParams['status'] = array_key_exists('status', $getParams) ? $getParams['status'] : 'OF300';
        $getParams['no_sertifikat'] = array_key_exists('no_sertifikat', $getParams) ? $getParams['no_sertifikat'] : null;
        $getParams['nama_produk'] = array_key_exists('nama_produk', $getParams) ? $getParams['nama_produk'] : null;
        $getParams['nama_pelaku_usaha'] = array_key_exists('nama_pelaku_usaha', $getParams) ? $getParams['nama_pelaku_usaha'] : null;

        $params = $this->validate(
            $getParams,
            [
                'page' => ['validators' => []],
                'size' => ['validators' => []],
                'status' => ['validators' => []],
                'no_sertifikat' => ['validators' => []],
                'nama_produk' => ['validators' => []],
                'nama_pelaku_usaha' => ['validators' => []]
            ]
        );

        if (empty($params['no_sertifikat']) && empty($params['nama_produk']) && empty($params['nama_pelaku_usaha'])) {
            throw new CustomErrorException(BaseResponse::INVALID_REQUEST, 'Permintaan tidak valid');
        }

        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop']);
        $client->setLogger($this->logger);

        try {
            $result = $client->local($params);
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse($result);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Data sertifikasi tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses pencarian sertifikasi gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function searchForeignCertificate()
    {
        $getParams = $this->request->get();

        $getParams['page'] = array_key_exists('page', $getParams) ? $getParams['page'] : 1;
        $getParams['size'] = array_key_exists('size', $getParams) ? $getParams['size'] : 10;

        $getParams['no_sertifikat'] = array_key_exists('no_sertifikat', $getParams) ? $getParams['no_sertifikat'] : null;
        $getParams['id'] = array_key_exists('id', $getParams) ? $getParams['id'] : null;
        $getParams['nama_produk'] = array_key_exists('nama_produk', $getParams) ? $getParams['nama_produk'] : null;
        $getParams['nama_pelaku_usaha'] = array_key_exists('nama_pelaku_usaha', $getParams) ? $getParams['nama_pelaku_usaha'] : null;

        $params = $this->validate(
            $getParams,
            [
                'page' => ['validators' => []],
                'size' => ['validators' => []],
                'no_sertifikat' => ['validators' => []],
                'id' => ['validators' => []],
                'nama_pelaku_usaha' => ['validators' => []],
                'nama_produk' => ['validators' => []],
            ]
        );

        if (empty($params['no_sertifikat']) && empty($params['nama_produk']) && empty($params['nama_pelaku_usaha'])) {
            throw new CustomErrorException(BaseResponse::INVALID_REQUEST, 'Permintaan tidak valid');
        }

        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop']);
        $client->setLogger($this->logger);

        try {
            $result = $client->foreign($params);
            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse($result);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Data sertifikasi luar tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses pencarian sertifikasi luar gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }

    public function rekapitulasiSehati()
    {
        $client = new SertifikasiHalalClient($this->config['sertifikasi_halal_prop_1']);
        $client->setLogger($this->logger);

        try {
            $result = $client->rekapitulasi_sehati();

            if ((!is_array($result) && is_string($result)) || empty($result)) {
                throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, $result);
            }

            return new CollectionPaginatedResponse(!$result['data'] ? [] : $result['data']);
        } catch (CustomErrorException $e) {
            throw new CustomErrorException(BaseResponse::DATA_NOT_AVAILABLE, "Rekapitulasi SEHATI tidak ditemukan", [$e->getMessages()]);
        } catch (\Exception $e) {
            throw new CustomErrorException(BaseResponse::UNEXPECTED_ERROR, "Proses mendapatkan rekapitulasi SEHATI gagal. Mohon coba beberapa saat lagi", [$e->getMessage()]);
        }
    }
}