<?php

namespace App\Models\Potensiwilayah;

use App\Models\BaseModel;

class KelokasiModel extends BaseModel
{

    public function dummy_data()
    {
        $db = \Config\Database::connect();
        $builder = $db->table('plokasi_main');
        $builder->select('*');

        return $builder->get()->getResult();
    }

    public function getKantorLocation($user_id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('users u');
        $builder->select('ps.lati, ps.longi, ps.satker');
        $builder->join('pdk_satker ps', 'ps.id = u.id_kantor');
        $builder->where('u.id', $user_id);
        
        return $builder->get()->getRowArray();
    }

    public function getLokasiKategori()
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_kate');
        $builder->select('id, nama_kat, ket');
        $builder->orderBy('nama_kat', 'ASC');
        
        return $builder->get()->getResultArray();
    }

    public function saveLokasiToMain($data)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_main');
        
        $insertData = [
            'id_kategori' => $data['id_kategori'] ?? null,
            'nama_lokasi' => $data['nama_lokasi'] ?? null,
            'alamat' => $data['alamat'] ?? null,
            'lati' => $data['lati'] ?? null,
            'longi' => $data['longi'] ?? null,
            'id_kantor' => $data['id_kantor'] ?? null,
            'place_id' => $data['place_id'] ?? null,
            'phone' => $data['phone'] ?? null,
            'website' => $data['website'] ?? null,
            'rating' => $data['rating'] ?? null,
            'id_rek' => $data['id_rek'] ?? null,
            'wkt_rek' => $data['wkt_rek'] ?? date('Y-m-d H:i:s')
        ];
        
        return $builder->insert($insertData);
    }

    public function checkLocationExists($place_id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_main');
        $builder->where('place_id', $place_id);
        
        return $builder->get()->getRowArray();
    }

    public function getSavedLocationsByKantor($id_kantor)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_main lm');
        $builder->select('lm.*, lk.nama_kat');
        $builder->join('lokasi_kate lk', 'lk.id = lm.id_kategori', 'left');
        $builder->where('lm.id_kantor', $id_kantor);
        $builder->orderBy('lm.wkt_rek', 'DESC');
        
        return $builder->get()->getResultArray();
    }

    public function updateLokasiMain($id, $data)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_main');
        
        $updateData = [];
        if (isset($data['id_kategori'])) $updateData['id_kategori'] = $data['id_kategori'];
        if (isset($data['nama_lokasi'])) $updateData['nama_lokasi'] = $data['nama_lokasi'];
        if (isset($data['alamat'])) $updateData['alamat'] = $data['alamat'];
        if (isset($data['phone'])) $updateData['phone'] = $data['phone'];
        if (isset($data['website'])) $updateData['website'] = $data['website'];
        if (isset($data['rating'])) $updateData['rating'] = $data['rating'];
        
        return $builder->where('id', $id)->update($updateData);
    }

    public function deleteLokasiMain($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_main');
        
        return $builder->where('id', $id)->delete();
    }

    public function getLokasiMainById($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_main lm');
        $builder->select('lm.*, lk.nama_kat');
        $builder->join('lokasi_kate lk', 'lk.id = lm.id_kategori', 'left');
        $builder->where('lm.id', $id);
        
        return $builder->get()->getRowArray();
    }

}