<?php

namespace App\Models;

use CodeIgniter\Model;

class ServiceZoneModel extends Model
{
    protected $table            = 'service_zones';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $allowedFields    = ['name', 'created_at', 'updated_at'];
    protected $useTimestamps   = true;

    /**
     * Check if point (lat, lng) is inside any service zone (using MySQL spatial).
     */
    public function isInServiceZone(float $lat, float $lng): bool
    {
        $sql = 'SELECT id FROM service_zones WHERE boundary IS NOT NULL AND ST_Contains(boundary, ST_GeomFromText(?, 4326)) LIMIT 1';
        $wkt = 'POINT(' . $lng . ' ' . $lat . ')';
        $result = $this->db->query($sql, [$wkt]);
        return $result->getNumRows() > 0;
    }
}
