<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ZoneRule extends Model
{
    protected $fillable = [
        'zone_id', 'city_id', 'road_width_min_m', 'road_width_max_m',
        'max_far', 'max_height_m', 'max_ground_coverage', 'open_space_pct',
        'front_setback_m', 'side_setback_m', 'rear_setback_m',
        'parking_ratio_ecs', 'rule_source', 'special_conditions',
        'version', 'is_active',
    ];

    protected $casts = [
        'is_active'            => 'boolean',
        'road_width_min_m'     => 'float',
        'road_width_max_m'     => 'float',
        'max_far'              => 'float',
        'max_height_m'         => 'float',
        'max_ground_coverage'  => 'float',
        'open_space_pct'       => 'float',
        'front_setback_m'      => 'float',
        'side_setback_m'       => 'float',
        'rear_setback_m'       => 'float',
        'parking_ratio_ecs'    => 'float',
        'version'              => 'integer',
    ];

    public function zone()
    {
        return $this->belongsTo(Zone::class);
    }

    public function city()
    {
        return $this->belongsTo(City::class);
    }

    public function feasibilityReports()
    {
        return $this->hasMany(FeasibilityReport::class);
    }

    /**
     * Human-readable road width range label.
     */
    public function getRoadWidthLabelAttribute(): string
    {
        $min = $this->road_width_min_m;
        $max = $this->road_width_max_m;

        if ($max === null) {
            return ">= {$min}m";
        }
        return "{$min}m – {$max}m";
    }
}
