<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use App\Models\Master\Provinsi;
use App\Models\Master\Kabupaten;
use App\Models\Master\Kecamatan;
use App\Models\Master\Sekolah;
use App\Models\Master\MasterTingkatSekolah;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;


class ApiController extends Controller
{
    public function getProvince()
    {
            $curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_URL => 'https://dapo.dikdasmen.go.id/rekap/dataSekolah?id_level_wilayah=0&kode_wilayah=000000&semester_id=20242',
              CURLOPT_RETURNTRANSFER    => true,
              CURLOPT_ENCODING          => '',
              CURLOPT_MAXREDIRS         => 10,
              CURLOPT_TIMEOUT           => 0,
              CURLOPT_FOLLOWLOCATION    => true,
              CURLOPT_HTTP_VERSION      => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST     => 'GET',
            ));

            $response = curl_exec($curl);

            curl_close($curl);

            $data   = json_decode($response);
            $_data  = [];
            foreach($data as $k => $val){
                $_data = Provinsi::updateOrCreate([
                    'kode_wilayah' => str_replace(' ', '', $val->kode_wilayah)
                ],[
                    'name' => str_replace('Prov. ','',$val->nama),
                    'kode_wilayah' => str_replace(' ', '', $val->kode_wilayah),
                ]);
            }

            echo "-- DONE --";
       
    }

    public function getKabupaten()
    {
        $prov = Provinsi::where('kode_wilayah','010000')->first();
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://dapo.dikdasmen.go.id/rekap/dataSekolah?id_level_wilayah=1&kode_wilayah=010000&semester_id=20242',
          CURLOPT_RETURNTRANSFER  => true,
          CURLOPT_ENCODING        => '',
          CURLOPT_MAXREDIRS       => 10,
          CURLOPT_TIMEOUT         => 0,
          CURLOPT_FOLLOWLOCATION  => true,
          CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST   => 'GET',
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $data   = json_decode($response);
            $_data  = [];

            
            foreach($data as $k => $val){
                $_data = Kabupaten::updateOrCreate([
                    'kode_wilayah'      => str_replace(' ', '', $val->kode_wilayah),
                ],[
                    'ms_provinsi_id'    => $prov->MsProvinsiId,
                    'name'              => str_replace('Kab. ','',$val->nama),
                    'kode_wilayah'      => str_replace(' ', '', $val->kode_wilayah),
                ]);
            }

            echo "-- DONE --";

    }

    public function getKecamatan()
    {
        //get kabupaten jakarta
        $prov = Provinsi::where('kode_wilayah','010000')->first();
        $kab = Kabupaten::where('ms_provinsi_id',$prov->MsProvinsiId)->get();
        foreach($kab as $data){
            $response = Http::timeout(300)->withoutVerifying()->get('https://dapo.dikdasmen.go.id/rekap/dataSekolah?id_level_wilayah=2&kode_wilayah='.$data->kode_wilayah.'&semester_id=20242');   

            $kec = json_decode($response->body());
            foreach($kec as $val){
                $_data = Kecamatan::updateOrCreate([
                    'kode_wilayah'      => str_replace(' ', '', $val->kode_wilayah),
                ],[
                    'ms_provinsi_id'    => $prov->MsProvinsiId,
                    'ms_kabupaten_id'   => $data->MsKabupatenId,
                    'name'              => str_replace('Kec. ','',$val->nama),
                    'kode_wilayah'      => str_replace(' ', '', $val->kode_wilayah),
                ]);
            }
        }

        echo "-- DONE --";
    }

    public function getSekolah()
    {

        $type = request()->type;

        $level = MasterTingkatSekolah::where('name',strtoupper($type))->first();
        //get kabupaten jakarta
        $prov = Provinsi::where('kode_wilayah','010000')->first();
        $kab = Kabupaten::where('ms_provinsi_id',$prov->MsProvinsiId)->get();
        foreach($kab as $data){
            $kec = Kecamatan::where('ms_kabupaten_id',$data->MsKabupatenId)->get();

            foreach($kec as $dataKec){
                $response = Http::timeout(300)->withoutVerifying()->get('https://dapo.dikdasmen.go.id/rekap/progresSP?id_level_wilayah=3&kode_wilayah='.$dataKec->kode_wilayah.'&semester_id=20242&bentuk_pendidikan_id='.$type); 

                $dataSekolah = json_decode($response->body());

                foreach($dataSekolah as $val){
                    $_data = Sekolah::updateOrCreate([
                        'npsn'                      => $val->npsn,
                    ],[
                        'ms_provinsi_id'               => $prov->MsProvinsiId,
                        'ms_kabupaten_id'              => $data->MsKabupatenId,
                        'ms_kecamatan_id'              => $dataKec->MsKecamatanId,
                        'ms_tingkat_sekolah_id'     => $level->MsTingkatSekolahId,    
                        'status'                    => $val->status_sekolah,
                        'nama_sekolah'              => $val->nama,
                        'npsn'                      => $val->npsn,
                    ]);
                }
            }
            
        }

        echo "-- DONE --";
    }

    public function scrappingMI(Request $request)
    {
        $prov = Provinsi::where('kode_wilayah','010000')->first();
        $kab = Kabupaten::where('ms_provinsi_id',$prov->MsProvinsiId)->get();
        $jenjang    = $request->input('jenjang', '3');         // SD    = 3
        $jenis      = $request->input('jenis', 'jf');            // jenjang formal
        //9 = MI, 10 = Mts
        $status     = $request->input('status');           // semua status
        $level      = $request->input('level', 'all');           // semua level
        $dataScrapping = [];

        foreach($kab as $data){
            $kec = Kecamatan::where('ms_kabupaten_id',$data->MsKabupatenId)->get();
            foreach($kec as $dataKec){

                $provinsi   = $request->input('provinsi', $dataKec->kode_wilayah);  // default Kep. Seribu Selatan


                $url    = "https://referensi.data.kemdikbud.go.id/pendidikan/dikdas/{$provinsi}/{$jenjang}/{$jenis}/{$status}/{$level}";

                $response = Http::timeout(300)->withoutVerifying()->get($url);
                // $client = new Client();
                // $res    = $client->get($url);
                $html   = $response->body();

                $crawler    = new Crawler($html);
                $table      = $crawler->filter('table')->first();

                

                $headers = $table->filter('tr')->first()->filter('th')->each(function ($node) {
                    return trim($node->text());
                });

                $table->filter('tr')->each(function ($tr, $i) use (&$dataScrapping, $headers) {
                    if ($i === 0) return; // skip header row
                    $row = $tr->filter('td')->each(function ($td) {
                        return trim($td->text());
                    });
                    if (count($row) === count($headers)) {
                        $dataScrapping[] = array_combine($headers, $row);
                    }
                });
                // dd($dataScrapping);
                if($status == 9){
                    $tingkat = 5;
                }elseif($status == 10){
                    $tingkat = 6;
                }
                foreach($dataScrapping as $val){
                    $_data = Sekolah::updateOrCreate([
                        'npsn'                      => $val['NPSN'],
                    ],[
                        'ms_provinsi_id'            => $prov->MsProvinsiId,
                        'ms_kabupaten_id'           => $data->MsKabupatenId,
                        'ms_kecamatan_id'           => $dataKec->MsKecamatanId,
                        'ms_tingkat_sekolah_id'     => $tingkat,
                        'status'                    => ucwords($val['Status']),
                        'nama_sekolah'              => $val['Nama Satuan Pendidikan'],
                        'alamat_sekolah'            => $val['Alamat'],
                        'npsn'                      => $val['NPSN'],
                    ]);
                }
            }
        }

         return response()->json([
            'message' => 'Scraping selesai',
            'jumlah_data' => count($dataScrapping),
            'data' => $dataScrapping,
        ]);
    }

    public function scrappingMA(Request $request)
    {
        $prov = Provinsi::where('kode_wilayah','010000')->first();
        $kab = Kabupaten::where('ms_provinsi_id',$prov->MsProvinsiId)->get();
        $jenjang    = $request->input('jenjang', '3');         // SD    = 3
        $jenis      = $request->input('jenis', 'jf');            // jenjang formal
        $status     = $request->input('status', '16');           // semua status
        $level      = $request->input('level', 'all');           // semua level
        $dataScrapping = [];

        foreach($kab as $data){
            $kec = Kecamatan::where('ms_kabupaten_id',$data->MsKabupatenId)->get();
            foreach($kec as $dataKec){

                $provinsi   = $request->input('provinsi', $dataKec->kode_wilayah);  // default Kep. Seribu Selatan


                $url    = "https://referensi.data.kemdikbud.go.id/pendidikan/dikmen/{$provinsi}/{$jenjang}/{$jenis}/{$status}/{$level}";

                $response = Http::timeout(300)->withoutVerifying()->get($url);
                // $client = new Client();
                // $res    = $client->get($url);
                $html   = $response->body();

                $crawler    = new Crawler($html);
                $table      = $crawler->filter('table')->first();

                

                $headers = $table->filter('tr')->first()->filter('th')->each(function ($node) {
                    return trim($node->text());
                });

                $table->filter('tr')->each(function ($tr, $i) use (&$dataScrapping, $headers) {
                    if ($i === 0) return; // skip header row
                    $row = $tr->filter('td')->each(function ($td) {
                        return trim($td->text());
                    });
                    if (count($row) === count($headers)) {
                        $dataScrapping[] = array_combine($headers, $row);
                    }
                });
                // dd($dataScrapping);
                foreach($dataScrapping as $val){
                    $_data = Sekolah::updateOrCreate([
                        'npsn'                      => $val['NPSN'],
                    ],[
                        'ms_provinsi_id'            => $prov->MsProvinsiId,
                        'ms_kabupaten_id'           => $data->MsKabupatenId,
                        'ms_kecamatan_id'           => $dataKec->MsKecamatanId,
                        'ms_tingkat_sekolah_id'     => 7,    
                        'status'                    => ucwords($val['Status']),
                        'nama_sekolah'              => $val['Nama Satuan Pendidikan'],
                        'alamat_sekolah'            => $val['Alamat'],
                        'npsn'                      => $val['NPSN'],
                    ]);
                }
            }
        }

         return response()->json([
            'message' => 'Scraping selesai',
            'jumlah_data' => count($dataScrapping),
            'data' => $dataScrapping,
        ]);
    }
}
