<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class SubscriptionPlanController extends Controller
{
    // Display a listing of the subscription plans
    public function index()
    {
        $plans = DB::table('subscription_plans')->get(); // Fetching plans directly from the database
        return view('subscription-plans.index', compact('plans'));
    }

    // Store a newly created subscription plan in storage
   public function store(Request $request)
{
    $request->validate([
        'name' => 'required|string|max:255',
        'description' => 'required|string',
        'price' => 'required|numeric',
        'user_type' => 'required|in:user,vendor,driver',
        'valid_for' => 'required|in:week,month,year', // Validation for valid_for field
        'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
    ]);

    $imagePath = null;
    if ($request->hasFile('image')) {
        $imagePath = $request->file('image')->store('subscription_plans', 'public');
    }

    DB::table('subscription_plans')->insert([
        'name' => $request->name,
        'description' => $request->description,
        'price' => $request->price,
        'user_type' => $request->user_type,
        'valid_for' => $request->valid_for, // Store the valid_for value
        'image' => $imagePath,
        'created_at' => now(),
        'updated_at' => now(),
    ]);

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


    // Display the specified subscription plan
    public function show($id)
    {
        $plan = DB::table('subscription_plans')->where('id', $id)->first(); // Fetching a single plan
        return view('subscription-plans.show', compact('plan'));
    }

    // Update the specified subscription plan in storage
    public function update(Request $request, $id)
{
    $request->validate([
        'name' => 'sometimes|required|string|max:255',
        'description' => 'sometimes|required|string',
        'price' => 'sometimes|required|numeric',
        'user_type' => 'sometimes|required|in:user,vendor,driver',
        'valid_for' => 'sometimes|required|in:week,month,year', // Validation for valid_for field
        'image' => 'sometimes|nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
    ]);

    $plan = DB::table('subscription_plans')->where('id', $id)->first();
    $imagePath = $plan->image;

    if ($request->hasFile('image')) {
        if ($imagePath) {
            Storage::disk('public')->delete($imagePath);
        }
        $imagePath = $request->file('image')->store('subscription_plans', 'public');
    }

    DB::table('subscription_plans')->where('id', $id)->update([
        'name' => $request->name ?? $plan->name,
        'description' => $request->description ?? $plan->description,
        'price' => $request->price ?? $plan->price,
        'user_type' => $request->user_type ?? $plan->user_type,
        'valid_for' => $request->valid_for ?? $plan->valid_for, // Update the valid_for value
        'image' => $imagePath,
        'updated_at' => now(),
    ]);

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


    // Remove the specified subscription plan from storage
    public function destroy($id)
    {
        $plan = DB::table('subscription_plans')->where('id', $id)->first(); // Fetching plan to delete
        if ($plan) {
            // Delete the associated image if it exists
            if ($plan->image) {
                Storage::disk('public')->delete($plan->image); // Delete the image from storage
            }
            // Delete the plan from the database
            DB::table('subscription_plans')->where('id', $id)->delete(); 
            return redirect()->route('subscription.index')->with('success', 'Plan deleted successfully.');
        }

        return redirect()->route('subscription.index')->with('error', 'Plan not found.');
    }
}
