<?php

namespace App\Http\Controllers;

use App\Models\App;
use App\Models\PricingPlan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PricingPlansController extends Controller
{
    public function index()
    {
        $plans = PricingPlan::latest()->get();
        $apps  = App::where('is_active', true)->orderBy('display_name')->get();
        return view('pricing-plans', compact('plans', 'apps'))->with('authUser', Auth::user());
    }

    public function store(Request $request)
    {
        $request->validate([
            'name'                => ['required', 'string', 'max:255'],
            'description'         => ['nullable', 'string'],
            'features'            => ['nullable', 'string'],
            'price'               => ['required', 'numeric', 'min:0'],
            'period'              => ['required', 'in:1month,3months,6months,1year'],
            'payment_processor'   => ['nullable', 'string', 'max:100'],
            'campaigns_limit'     => ['nullable', 'integer', 'min:0'],
            'messages_limit'      => ['nullable', 'integer', 'min:0'],
            'contacts_limit'      => ['nullable', 'integer', 'min:0'],
            'plugins'             => ['nullable', 'string'],
        ]);

        PricingPlan::create([
            'name'              => $request->name,
            'description'       => $request->description,
            'features'          => $request->features,
            'price'             => $request->price,
            'period'            => $request->period,
            'payment_processor' => $request->payment_processor,
            'campaigns_limit'   => $request->campaigns_limit ?? 0,
            'messages_limit'    => $request->messages_limit  ?? 0,
            'contacts_limit'    => $request->contacts_limit  ?? 0,
            'plugins'           => $request->plugins,
            'is_active'         => true,
        ]);

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

    public function update(Request $request, PricingPlan $pricingPlan)
    {
        $request->validate([
            'name'              => ['required', 'string', 'max:255'],
            'description'       => ['nullable', 'string'],
            'features'          => ['nullable', 'string'],
            'price'             => ['required', 'numeric', 'min:0'],
            'period'            => ['required', 'in:1month,3months,6months,1year'],
            'payment_processor' => ['nullable', 'string', 'max:100'],
            'campaigns_limit'   => ['nullable', 'integer', 'min:0'],
            'messages_limit'    => ['nullable', 'integer', 'min:0'],
            'contacts_limit'    => ['nullable', 'integer', 'min:0'],
            'plugins'           => ['nullable', 'string'],
        ]);

        $pricingPlan->update([
            'name'              => $request->name,
            'description'       => $request->description,
            'features'          => $request->features,
            'price'             => $request->price,
            'period'            => $request->period,
            'payment_processor' => $request->payment_processor,
            'campaigns_limit'   => $request->campaigns_limit ?? 0,
            'messages_limit'    => $request->messages_limit  ?? 0,
            'contacts_limit'    => $request->contacts_limit  ?? 0,
            'plugins'           => $request->plugins,
        ]);

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

    public function destroy(PricingPlan $pricingPlan)
    {
        $pricingPlan->delete();
        return redirect()->route('pricing-plans')->with('success', 'Plan deleted.');
    }

    public function toggleActive(PricingPlan $pricingPlan)
    {
        $pricingPlan->update(['is_active' => !$pricingPlan->is_active]);
        return redirect()->route('pricing-plans')->with('success', 'Plan status updated.');
    }
}
