<?php

namespace App\Domain\Facility\Models;

use App\Domain\Shared\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Domain\Shared\Traits\ScopedThroughBranch;

class SpaceSegment extends Model
{
    use HasUuid, ScopedThroughBranch;

    /**
     * The parent whose branch this row inherits. A line item has no
     * branch of its own and must not grow one — a denormalised copy
     * here is a second source of truth that drifts the first time the
     * parent moves.
     */
    protected static string $branchScopeRelation = 'layout';

    protected $fillable = [
        'space_layout_id',
        'code',
        'name',
        'name_ar',
        'row_index',
        'col_index',
        'lane_number',
        'area_sqm',
        'is_available',
        'capacity',
        'sort_order',
        'metadata',
    ];

    protected function casts(): array
    {
        return [
            'row_index' => 'integer',
            'col_index' => 'integer',
            'lane_number' => 'integer',
            'area_sqm' => 'decimal:2',
            'is_available' => 'boolean',
            'capacity' => 'integer',
            'sort_order' => 'integer',
            'metadata' => 'array',
        ];
    }

    public function layout(): BelongsTo
    {
        return $this->belongsTo(SpaceLayout::class, 'space_layout_id');
    }

    public function scopeAvailable($query)
    {
        return $query->where('is_available', true);
    }
}
