<?php

namespace App\Models;

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

class Tenant extends Model
{
    protected $fillable = [
        'package_id', 'name', 'slug', 'email', 'phone', 'logo_path', 'status', 'timezone',
        'currency', 'employee_limit', 'branch_limit', 'device_limit', 'expires_at', 'settings',
    ];

    protected $casts = [
        'expires_at' => 'datetime',
        'settings' => 'array',
    ];

    public function package(): BelongsTo
    {
        return $this->belongsTo(Package::class);
    }

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

    public function hasModuleAccess(?string $module): bool
    {
        if (!$module || !$this->package) {
            return true;
        }

        return in_array($module, $this->package->features ?? [], true);
    }
}
