<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SiteSetting extends Model
{
    public const MAINTENANCE_MODE = 'maintenance_mode';
    public const MAINTENANCE_TITLE = 'maintenance_title';
    public const MAINTENANCE_MESSAGE = 'maintenance_message';
    public const PAYMENT_MODE = 'payment_mode';
    public const PAYMENT_MODE_ENABLED = 'payment_mode_enabled';
    public const FREE_TRIAL_DAYS = 'free_trial_days';
    public const SUBSCRIPTION_GRACE_DAYS = 'subscription_grace_days';
    public const ALLOW_FREE_ACCESS = 'allow_free_access';
    public const SUPPORT_EMAIL = 'support_email';
    public const SUPPORT_AUTO_REPLY_MESSAGE = 'support_auto_reply_message';
    public const EMAIL_VERIFICATION_ENABLED = 'email_verification_enabled';

    protected $fillable = [
        'key',
        'value',
    ];

    public static function getValue(string $key, ?string $default = null): ?string
    {
        return static::query()
            ->where('key', $key)
            ->value('value') ?? $default;
    }

    public static function setValue(string $key, ?string $value): self
    {
        $setting = static::query()->updateOrCreate(
            ['key' => $key],
            ['value' => $value]
        );

        return $setting;
    }

    public static function maintenanceModeEnabled(): bool
    {
        return static::getValue(static::MAINTENANCE_MODE, 'off') === 'on';
    }

    public static function paymentModeEnabled(): bool
    {
        return static::getValue(static::PAYMENT_MODE_ENABLED, static::getValue(static::PAYMENT_MODE, 'off')) === 'on';
    }

    public static function allowFreeAccess(): bool
    {
        return static::getValue(static::ALLOW_FREE_ACCESS, 'on') === 'on';
    }

    public static function freeTrialDays(): int
    {
        return max(0, (int) static::getValue(static::FREE_TRIAL_DAYS, '0'));
    }

    public static function subscriptionGraceDays(): int
    {
        return max(0, (int) static::getValue(static::SUBSCRIPTION_GRACE_DAYS, '0'));
    }

    public static function paymentSettings(): array
    {
        return [
            'payment_mode_enabled' => static::paymentModeEnabled() ? 'on' : 'off',
            'free_trial_days' => static::freeTrialDays(),
            'subscription_grace_days' => static::subscriptionGraceDays(),
            'allow_free_access' => static::allowFreeAccess() ? 'on' : 'off',
        ];
    }

    public static function maintenanceSettings(): array
    {
        return [
            'mode' => static::getValue(static::MAINTENANCE_MODE, 'off'),
            'title' => static::getValue(static::MAINTENANCE_TITLE, 'We are currently under maintenance'),
            'message' => static::getValue(static::MAINTENANCE_MESSAGE, 'ViZuTrade is getting a quick upgrade. Please check back soon while our team improves your trading workspace.'),
        ];
    }

    public static function supportEmail(): string
    {
        return static::getValue(static::SUPPORT_EMAIL, config('mail.from.address', 'support@vizutrade.com'));
    }

    public static function supportAutoReplyMessage(): string
    {
        return static::getValue(
            static::SUPPORT_AUTO_REPLY_MESSAGE,
            'Thank you for contacting ViZuTrade support. We have received your ticket and our team will review it as soon as possible.'
        );
    }

    public static function emailVerificationEnabled(): bool
    {
        return static::getValue(static::EMAIL_VERIFICATION_ENABLED, 'on') === 'on';
    }
}
