<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Services\PremiumFeatures;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;

class PremiumController extends Controller
{
    /** Kullanıcının premium kozmetik ayarlarını kaydet */
    public function saveCosmetics(Request $request)
    {
        $user = auth()->user();

        if (!$user->isPremium()) {
            return back()->with('error', 'Bu özellik için premium üyelik gerekiyor.');
        }

        $validated = $request->validate([
            'comment_bg'        => 'nullable|string|in:' . implode(',', array_keys(PremiumFeatures::COMMENT_BACKGROUNDS)),
            'comment_glow'      => 'nullable|string|in:' . implode(',', array_keys(PremiumFeatures::COMMENT_GLOWS)),
            'username_color'    => 'nullable|string|in:' . implode(',', array_keys(PremiumFeatures::USERNAME_COLORS)),
            'username_effect'   => 'nullable|string|in:' . implode(',', array_keys(PremiumFeatures::USERNAME_EFFECTS)),
            'profile_frame'     => 'nullable|string|in:' . implode(',', array_keys(PremiumFeatures::PROFILE_FRAMES)),
            'profile_badge'     => 'nullable|string|max:32',
            'profile_bg'        => 'nullable|string|in:' . implode(',', array_keys(PremiumFeatures::PROFILE_BACKGROUNDS)),
            'gif_avatar'        => 'nullable|url|max:500',
            'profile_music_url' => 'nullable|url|max:500',
            'comment_signature' => 'nullable|string|max:100',
            'entry_effect'      => 'nullable|string|in:' . implode(',', array_keys(PremiumFeatures::ENTRY_EFFECTS)),
            'animated_banner'   => 'nullable|boolean',
        ]);

        // Her alanı sadece ilgili perk varsa kaydet
        $updates = [];

        if ($user->hasPerk('comment_bg')) {
            $updates['comment_bg'] = $validated['comment_bg'] ?? null;
        }
        if ($user->hasPerk('comment_glow')) {
            $updates['comment_glow'] = $validated['comment_glow'] ?? null;
        }
        if ($user->hasPerk('username_color')) {
            $updates['username_color'] = $validated['username_color'] ?? null;
        }
        if ($user->hasPerk('username_effect')) {
            $updates['username_effect'] = $validated['username_effect'] ?? null;
        }
        if ($user->hasPerk('profile_frame')) {
            $updates['profile_frame'] = $validated['profile_frame'] ?? null;
        }
        if ($user->hasPerk('profile_badge')) {
            $updates['profile_badge'] = $validated['profile_badge'] ?? null;
        }
        if ($user->hasPerk('profile_bg')) {
            $updates['profile_bg'] = $validated['profile_bg'] ?? null;
        }
        if ($user->hasPerk('gif_avatar')) {
            $updates['gif_avatar'] = $validated['gif_avatar'] ?? null;
        }
        if ($user->hasPerk('profile_music') && Schema::hasColumn('users', 'profile_music_url')) {
            $updates['profile_music_url'] = $validated['profile_music_url'] ?? null;
        }
        if ($user->hasPerk('comment_signature')) {
            $updates['comment_signature'] = $validated['comment_signature'] ?? null;
        }
        if ($user->hasPerk('entry_effect')) {
            $updates['entry_effect'] = $validated['entry_effect'] ?? null;
        }
        if ($user->hasPerk('animated_banner')) {
            $updates['animated_banner'] = $request->boolean('animated_banner');
        }

        if (!empty($updates)) {
            $user->update($updates);
        }

        return back()->with('success', 'Premium ayarların kaydedildi!');
    }

    /** Public plans/pricing sayfası */
    public function plans()
    {
        $plans = \App\Models\MembershipPlan::where('is_active', true)
            ->where('is_public', true)
            ->where(fn($q) => $q->whereNull('visible_until')->orWhere('visible_until', '>', now()))
            ->orderBy('sort_order')
            ->get();

        $allFeatures = PremiumFeatures::grouped();

        return view('frontend.premium.plans', compact('plans', 'allFeatures'));
    }
}
