<?php

namespace Kylin\Guardian\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kylin\Core\Models\BaseModel;
use Kylin\User\Models\User;

class GuardianBinding extends BaseModel
{
    protected $table = 'kylin_guardian_bindings';

    protected $fillable = [
        'guardian_id',
        'minor_user_id',
        'relationship',
        'status',
        'play_limit',
        'spending_limit',
        'notify_settings',
    ];

    protected $casts = [
        'play_limit' => 'array',
        'spending_limit' => 'array',
        'notify_settings' => 'array',
    ];

    /**
     * 绑定状态常量
     */
    public const STATUS_ACTIVE = 'active';
    public const STATUS_DISABLED = 'disabled';

    /**
     * 关联监护人
     */
    public function guardian(): BelongsTo
    {
        return $this->belongsTo(Guardian::class);
    }

    /**
     * 关联未成年人用户
     */
    public function minorUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'minor_user_id');
    }

    /**
     * 绑定是否激活
     */
    public function isActive(): bool
    {
        return $this->status === self::STATUS_ACTIVE;
    }

    /**
     * 获取每日游戏时长限制（分钟）
     */
    public function getDailyPlayMinutes(): int
    {
        return $this->play_limit['daily_minutes']
            ?? config('kylin-guardian.default_play_limit.daily_minutes', 90);
    }

    /**
     * 获取允许游戏时段
     *
     * @return array<int, array{start: string, end: string}>
     */
    public function getAllowedPeriods(): array
    {
        return $this->play_limit['allowed_periods']
            ?? config('kylin-guardian.default_play_limit.allowed_periods', []);
    }

    /**
     * 获取每日消费上限（元）
     */
    public function getDailySpendingMax(): float
    {
        return (float) ($this->spending_limit['daily_max']
            ?? config('kylin-guardian.default_spending_limit.daily_max', 50));
    }

    /**
     * 获取月度消费上限（元）
     */
    public function getMonthlySpendingMax(): float
    {
        return (float) ($this->spending_limit['monthly_max']
            ?? config('kylin-guardian.default_spending_limit.monthly_max', 200));
    }

    /**
     * 获取单次消费上限（元）
     */
    public function getSingleSpendingMax(): float
    {
        return (float) ($this->spending_limit['single_max']
            ?? config('kylin-guardian.default_spending_limit.single_max', 20));
    }

    /**
     * 是否需要登录通知
     */
    public function shouldNotifyOnLogin(): bool
    {
        return $this->notify_settings['on_login']
            ?? config('kylin-guardian.default_notify_settings.on_login', true);
    }

    /**
     * 是否需要消费通知
     */
    public function shouldNotifyOnSpending(): bool
    {
        return $this->notify_settings['on_spending']
            ?? config('kylin-guardian.default_notify_settings.on_spending', true);
    }

    /**
     * 是否需要时长预警通知
     */
    public function shouldNotifyOnTimeWarning(): bool
    {
        return $this->notify_settings['on_time_warning']
            ?? config('kylin-guardian.default_notify_settings.on_time_warning', true);
    }

    /**
     * 是否需要强制下线通知
     */
    public function shouldNotifyOnForceOffline(): bool
    {
        return $this->notify_settings['on_force_offline']
            ?? config('kylin-guardian.default_notify_settings.on_force_offline', true);
    }

    /**
     * 获取通知渠道
     *
     * @return array<int, string>
     */
    public function getNotifyChannels(): array
    {
        return $this->notify_settings['channels']
            ?? config('kylin-guardian.default_notify_settings.channels', ['push']);
    }
}
