<?php

namespace Modules\Bms\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class BmsVenue extends Model
{
    protected $table = 'bms_venues';

    protected $fillable = [
        'business_id', 'location_id', 'area_id', 'name', 'name_ar', 'code',
        'address', 'city', 'phone', 'description', 'working_hours', 'is_active', 'sort_order',
    ];

    protected $casts = [
        'working_hours' => 'array',
        'is_active'     => 'boolean',
        'sort_order'    => 'integer',
    ];

    /** Effective hours for this venue (its custom set, or the business default). */
    public function effectiveHours(): array
    {
        $wh = $this->working_hours;
        if (! $wh || ! empty($wh['use_default'])) {
            $stored = BmsSetting::get((int) $this->business_id, 'default_working_hours');
            return $stored ?: ['days' => [0, 1, 2, 3, 4, 5, 6], 'open' => '10:00', 'close' => '22:00'];
        }
        return [
            'days'  => array_map('intval', (array) ($wh['days'] ?? [0, 1, 2, 3, 4, 5, 6])),
            'open'  => $wh['open'] ?? '10:00',
            'close' => $wh['close'] ?? '22:00',
        ];
    }

    public function area(): BelongsTo
    {
        return $this->belongsTo(BmsArea::class, 'area_id');
    }

    public function halls(): HasMany
    {
        return $this->hasMany(BmsHall::class, 'venue_id');
    }

    public function packages(): HasMany
    {
        return $this->hasMany(BmsPackage::class, 'venue_id');
    }
}
