<?php

namespace App\Models\Pesaing;

use App\Models\BaseModel;

class KuralokasiModel extends BaseModel
{
    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 saveLokasiPesaing($data)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        
        $insertData = [
            'nama_pesaing' => $data['nama_pesaing'] ?? null,
            'alamat' => $data['alamat'] ?? null,
            'lati' => $data['lati'] ?? null,
            'longi' => $data['longi'] ?? null,
            'place_id' => $data['place_id'] ?? null,
            'phone' => $data['phone'] ?? null,
            'website' => $data['website'] ?? null,
            'rating' => $data['rating'] ?? null,
            'id_kantor' => $data['id_kantor'] ?? 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)
    {
        if (empty($place_id)) {
            return false;
        }
        
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        $builder->where('place_id', $place_id);
        
        return $builder->get()->getRowArray();
    }

    public function getSavedLocationsByKantor($id_kantor)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        $builder->select('*');
        $builder->where('id_kantor', $id_kantor);
        $builder->orderBy('wkt_rek', 'DESC');
        
        return $builder->get()->getResultArray();
    }

    public function updateLokasiPesaing($id, $data)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        
        $updateData = [];
        if (isset($data['nama_pesaing'])) $updateData['nama_pesaing'] = $data['nama_pesaing'];
        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 deleteLokasiPesaing($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        
        return $builder->where('id', $id)->delete();
    }

    public function getLokasiPesaingById($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        $builder->select('*');
        $builder->where('id', $id);
        
        return $builder->get()->getRowArray();
    }
}
