<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Cities;
use App\Models\PricingPlan;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\JsonResponse;
class PlanController extends Controller
{
    public function index()
    {
        $plans = PricingPlan::orderBy('created_at', 'desc')->get();
        
        return view('admin.plans.index', compact('plans'));

    }
    public function create(){
        $plan = "";
        return view('admin.plans.create', compact('plan'));

    }

    public function store(Request $request)
    {
        $rules = [
            'name' => 'required|string',
            'name_ar' => 'required|string',
            'duration' => 'required|in:monthly,quarterly,yearly',
            'type' => 'required|in:free,basic,premium',
            'price' => 'required|string',
            'currency' => 'required|string',
            'description' => 'required|string',
            'description_ar' => 'required|string',
        ];
    
        $validator = Validator::make($request->all(), $rules);
    
        if ($validator->fails()) {
            return response()->json([
                'status' => 'error',
                'errors' => $validator->errors(),
            ], 422);
        }
    
        if ($request->id) {
            $plan = PricingPlan::find($request->id);
    
            if (!$plan) {
                return response()->json(['status' => 'error', 'message' => 'Plan not found'], 404);
            }
    
            $existingCity = PricingPlan::where('name', $request->name)
                ->where('id', '!=', $request->id)
                ->first();
    
            if ($existingCity) {
                return response()->json([
                    'status' => 'error',
                    'errors' => [
                        'name' => ['The Plan name already exists.']
                    ]
                ], 422);
            }
    
            $plan->name = $request->name;
            $plan->name_ar = $request->name_ar;
            $plan->duration = $request->duration;
            $plan->type = $request->type;
            $plan->price = $request->price;
            $plan->currency = $request->currency;
            $plan->description = $request->description;
            $plan->description_ar = $request->description_ar;
            $plan->max_request_count = $request->max_request_count;
            $plan->save();
    
            return response()->json(['status' => 'success', 'message' => 'Plan updated successfully']);
        } else {
            $existingCity = PricingPlan::where('name', $request->name)->first();
    
            if ($existingCity) {
                return response()->json([
                    'status' => 'error',
                    'errors' => [
                        'name' => ['The Plan name already exists.']
                    ]
                ], 422);
            }
    
            $plan = PricingPlan::create([
                'name' => $request->name,
                'name_ar' => $request->name_ar,
                'duration' => $request->duration,
                'price' => $request->price,
                'currency' => $request->currency,
                'description' => $request->description,
                'description_ar' => $request->description_ar,
                'max_request_count' => $request->max_request_count,
                'status' => $request->status,
            ]);
    
            return response()->json(['status' => 'success', 'message' => 'Plan added successfully', 'emirate' => $plan]);
        }
    }
    


  
    public function edit($id) {
        $plan = PricingPlan::findOrFail($id);
        return view('admin.plans.create', compact('plan'));
    }

    public function destroy($id): JsonResponse
    {
        try {
            $emirate = PricingPlan::findOrFail($id);           
            $emirate->delete();
            return response()->json([
                'status' => 'success',
                'message' => 'Plan deleted successfully.'
            ], 200);
        } catch (ModelNotFoundException $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Plan not found.'
            ], 404);
        } catch (Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'An error occurred while deleting the company.'
            ], 500);
        }
    }

    public function statusChange(Request $request, $id) {
        $plan = PricingPlan::find($id);

        if (!$plan) {
            return response()->json([
                'status' => 'error',
                'message' => 'Plan not found.'
            ], 404);
        }
    
        $plan->status = $request->status;
        $plan->save();
    
        return response()->json([
            'status' => 'success',
            'message' => 'Plan status updated successfully.'
        ]);
    }
}
