<?php

namespace App\Models;

use CodeIgniter\Model;

class TransportRouteStopModel extends Model
{
    protected $table         = 'transport_route_stops';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';

    protected $allowedFields = [
        'route_id', 'location_id', 'stop_order', 'estimated_arrival_offset_minutes', 'notes',
    ];

    public function getForRoute(int $routeId): array
    {
        return $this->select('transport_route_stops.*, transport_locations.name AS location_name, transport_locations.latitude, transport_locations.longitude')
            ->join('transport_locations', 'transport_locations.id = transport_route_stops.location_id', 'left')
            ->where('route_id', $routeId)
            ->orderBy('stop_order', 'ASC')
            ->findAll();
    }

    public function deleteForRoute(int $routeId): void
    {
        $this->where('route_id', $routeId)->delete();
    }
}
