<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Plan;
use Illuminate\Http\Request;

class PlanController extends Controller
{
    public function index()
    {
        $plans = Plan::all();
        return view('admin.plans.index', compact('plans'));
    }

    public function show(Plan $plan)
    {
        return view('admin.plans.show', compact('plan'));
    }

    public function create()
    {
        return view('admin.plans.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'price_inr' => 'required|numeric',
            'price_usd' => 'required|numeric',
            'duration_days' => 'required|integer',
            'product_limit' => 'required|integer',
            'product_image_limit' => 'required|integer|min:1',
            'inquiry_limit' => 'required|integer',
            'features_text' => 'nullable|string',
            'commission_rate' => 'nullable|numeric|min:0|max:100',
            'is_active' => 'nullable',
        ]);

        $data = $request->all();
        $data['slug'] = \Illuminate\Support\Str::slug($request->name);
        
        // Convert features_text to array
        if ($request->has('features_text')) {
            $data['features'] = array_filter(explode("\n", str_replace("\r", "", $request->features_text)));
        }

        // Handle boolean checkboxes
        $data['is_active'] = $request->has('is_active');
        $data['show_contact_details'] = $request->has('show_contact_details');
        $data['view_buyer_contact'] = $request->has('view_buyer_contact');
        $data['is_featured'] = $request->has('is_featured');
        $data['has_verified_badge'] = $request->has('has_verified_badge');
        $data['allow_microsite'] = $request->has('allow_microsite');

        Plan::create($data);

        return redirect()->route('admin.plans.index')->with('success', 'Plan created successfully.');
    }

    public function edit(Plan $plan)
    {
        return view('admin.plans.edit', compact('plan'));
    }

    public function update(Request $request, Plan $plan)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'price_inr' => 'required|numeric',
            'price_usd' => 'required|numeric',
            'duration_days' => 'required|integer',
            'product_limit' => 'required|integer',
            'product_image_limit' => 'required|integer|min:1',
            'inquiry_limit' => 'required|integer',
            'features_text' => 'nullable|string',
            'commission_rate' => 'nullable|numeric|min:0|max:100',
            'is_active' => 'nullable',
        ]);

        $data = $request->all();
        $data['slug'] = \Illuminate\Support\Str::slug($request->name);
        
        // Convert features_text to array
        if ($request->has('features_text')) {
            $data['features'] = array_filter(explode("\n", str_replace("\r", "", $request->features_text)));
        }

        // Handle boolean checkboxes
        $data['is_active'] = $request->has('is_active');
        $data['show_contact_details'] = $request->has('show_contact_details');
        $data['view_buyer_contact'] = $request->has('view_buyer_contact');
        $data['is_featured'] = $request->has('is_featured');
        $data['has_verified_badge'] = $request->has('has_verified_badge');
        $data['allow_microsite'] = $request->has('allow_microsite');

        $plan->update($data);

        return redirect()->route('admin.plans.index')->with('success', 'Plan updated successfully.');
    }

    public function destroy(Plan $plan)
    {
        // Check if any subscriptions are using this plan
        if ($plan->subscriptions()->exists()) {
            return redirect()->route('admin.plans.index')
                ->with('error', 'Cannot delete plan. ' . $plan->subscriptions()->count() . ' subscription(s) are currently using this plan.');
        }
        
        $plan->delete();
        return redirect()->route('admin.plans.index')->with('success', 'Plan deleted successfully.');
    }
}
