<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MembershipPlan;
use App\Services\PremiumFeatures;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class PlanController extends Controller
{
    public function index()
    {
        $plans = MembershipPlan::orderBy('sort_order')->get();
        return view('admin.plans.index', compact('plans'));
    }

    public function create()
    {
        $allPerks = PremiumFeatures::grouped();
        return view('admin.plans.create', compact('allPerks'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name'          => 'required|string|max:255',
            'description'   => 'nullable|string',
            'price'         => 'required|numeric|min:0',
            'purchase_link' => 'nullable|url|max:1000',
            'duration_days' => 'required|integer|min:1',
            'trial_days'    => 'nullable|integer|min:0',
            'badge_label'   => 'nullable|string|max:32',
            'accent_color'  => 'nullable|string|max:16',
            'features'      => 'nullable|array',
            'features.*'    => 'string',
            'perks'         => 'nullable|array',
            'is_active'     => 'boolean',
            'is_public'     => 'boolean',
            'visible_until' => 'nullable|date',
            'sort_order'    => 'integer',
        ]);

        $data['slug']          = Str::slug($data['name']);
        $data['is_active']     = $request->boolean('is_active');
        $data['is_public']     = $request->boolean('is_public', true);
        $data['trial_days']    = (int) ($request->input('trial_days', 0));
        $data['purchase_link'] = $request->filled('purchase_link') ? $request->input('purchase_link') : null;
        $data['badge_label']   = $request->filled('badge_label') ? $request->input('badge_label') : null;
        $data['accent_color']  = $request->filled('accent_color') ? $request->input('accent_color') : null;
        $data['visible_until'] = $request->filled('visible_until') ? $request->input('visible_until') : null;
        $data['features']      = array_values(array_filter($request->features ?? []));

        $perks = [];
        foreach (array_keys(PremiumFeatures::ALL) as $key) {
            $perks[$key] = in_array($key, $request->input('perks', []));
        }
        $data['perks'] = $perks;

        MembershipPlan::create($data);
        return redirect()->route('admin.plans.index')->with('success', 'Plan eklendi.');
    }

    public function edit(MembershipPlan $plan)
    {
        $allPerks = PremiumFeatures::grouped();
        return view('admin.plans.edit', compact('plan', 'allPerks'));
    }

    public function update(Request $request, MembershipPlan $plan)
    {
        $data = $request->validate([
            'name'          => 'required|string|max:255',
            'description'   => 'nullable|string',
            'price'         => 'required|numeric|min:0',
            'purchase_link' => 'nullable|url|max:1000',
            'duration_days' => 'required|integer|min:1',
            'trial_days'    => 'nullable|integer|min:0',
            'badge_label'   => 'nullable|string|max:32',
            'accent_color'  => 'nullable|string|max:16',
            'features'      => 'nullable|array',
            'features.*'    => 'string',
            'perks'         => 'nullable|array',
            'is_active'     => 'boolean',
            'is_public'     => 'boolean',
            'visible_until' => 'nullable|date',
            'sort_order'    => 'integer',
        ]);

        $data['is_active']     = $request->boolean('is_active');
        $data['is_public']     = $request->boolean('is_public', true);
        $data['trial_days']    = (int) ($request->input('trial_days', 0));
        $data['purchase_link'] = $request->filled('purchase_link') ? $request->input('purchase_link') : null;
        $data['badge_label']   = $request->filled('badge_label') ? $request->input('badge_label') : null;
        $data['accent_color']  = $request->filled('accent_color') ? $request->input('accent_color') : null;
        $data['visible_until'] = $request->filled('visible_until') ? $request->input('visible_until') : null;
        $data['features']      = array_values(array_filter($request->features ?? []));

        $perks = [];
        foreach (array_keys(PremiumFeatures::ALL) as $key) {
            $perks[$key] = in_array($key, $request->input('perks', []));
        }
        $data['perks'] = $perks;

        $plan->update($data);
        return redirect()->route('admin.plans.index')->with('success', 'Plan güncellendi.');
    }

    public function destroy(MembershipPlan $plan)
    {
        $plan->delete();
        return redirect()->route('admin.plans.index')->with('success', 'Plan silindi.');
    }
}
