<?php

declare(strict_types=1);

namespace App\Models;

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

class Package extends Model
{
    protected $guarded = [];

    protected function casts(): array
    {
        return [
            'price' => 'decimal:2',
            'group_surcharge' => 'decimal:2',
            'skips_lead_time' => 'boolean',
            'allows_group' => 'boolean',
            'is_active' => 'boolean',
        ];
    }

    public function purchases(): HasMany
    {
        return $this->hasMany(PackagePurchase::class);
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }

    public function hasAgeLimit(): bool
    {
        return $this->min_age !== null || $this->max_age !== null;
    }

    /** Young Creators is 16 to 25; everything else is open to anyone. */
    public function acceptsAge(?int $age): bool
    {
        if (! $this->hasAgeLimit()) {
            return true;
        }

        if ($age === null) {
            return false;
        }

        return ($this->min_age === null || $age >= $this->min_age)
            && ($this->max_age === null || $age <= $this->max_age);
    }
}
