<?php

namespace App\Models;

use CodeIgniter\Model;

class DriverLocationModel extends Model
{
    protected $table            = 'driver_locations';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $allowedFields    = ['user_id', 'latitude', 'longitude', 'updated_at'];
    protected $useTimestamps    = false;

    public function upsertLocation(int $userId, float $latitude, float $longitude): bool
    {
        $row = $this->where('user_id', $userId)->first();
        $data = [
            'user_id'    => $userId,
            'latitude'   => $latitude,
            'longitude'  => $longitude,
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        if ($row) {
            return $this->update($row->id, $data);
        }
        return (bool) $this->insert($data);
    }
}
