<?php

namespace Modules\MedOrganization\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * 1:1 satellite of core business_locations (spec §5 facility). Hierarchy is
 * expressed through parent_location_id → business_locations.id (decision D5).
 */
class OrgFacilityProfile extends Model
{
    protected $table = 'org_facility_profiles';

    protected $guarded = ['id'];

    protected $casts = [
        'working_hours'   => 'array',
        'capacity'        => 'array',
        'modules_enabled' => 'array',
        'metadata'        => 'array',
        'geo_lat'         => 'float',
        'geo_lng'         => 'float',
    ];

    public function location()
    {
        return $this->belongsTo(\App\BusinessLocation::class, 'business_location_id');
    }

    public function parentLocation()
    {
        return $this->belongsTo(\App\BusinessLocation::class, 'parent_location_id');
    }

    /** Profile of the parent facility (if the parent location has one). */
    public function parentProfile()
    {
        return $this->belongsTo(self::class, 'parent_location_id', 'business_location_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }

    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }
}
