<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Models\CouponDiscountModel;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Log;


use Carbon\Carbon;
class CouponController extends Controller
{
    
    public function createCoupons(Request $request)
    {
        try {
            // Validate data
            $validatedData = $request->validate([
                'coupon_code' => 'required|string',
                'usage_limit' => 'nullable|string',
                'apply_coupon_on_product' => 'nullable|string',
                'discount' => 'nullable|string',
                'end_date' => 'nullable|string',
                'minimum_value' => 'nullable|string',
                'orderValues' => 'nullable|string',
                'orderQuality' => 'nullable|string',
                'start_date' => 'nullable|string',
                'time' => 'nullable|string',
                'upto_value' => 'nullable|string',
                'coupon_functionality' => 'nullable|string',
                'discount_type' => 'nullable|string',
            ]);

            // Insert into database
            CouponDiscountModel::create($validatedData);

            return response()->json(['message' => 'Coupon created successfully!'], 201);
        } catch (ValidationException $e) {
            return response()->json(['errors' => $e->errors()], 400); // Handle validation errors
        } catch (\Exception $e) {
            return response()->json(['error' => 'Internal Server Error: ' . $e->getMessage()], 500); // Handle other errors
        }
    }
      public function getCouponData($id)  {
        $couponDetails = CouponDiscountModel::find($id);
        return response()->json($couponDetails);
    }
    public function updateCouponData(Request $request, $id) {
    try {
        // Validate incoming data
        $request->validate([
                'coupon_code' => 'required|string',
                'usage_limit' => 'nullable|string',
                'apply_coupon_on_product' => 'nullable|string',
                'discount' => 'nullable|string',
                'end_date' => 'nullable|string',
                'minimum_value' => 'nullable|string',
                'orderValues' => 'nullable|string',
                'orderQuality' => 'nullable|string',
                'start_date' => 'nullable|string',
                'time' => 'nullable|string',
                'upto_value' => 'nullable|string',
                'coupon_functionality' => 'nullable|string',
                'discount_type' => 'nullable|string',
               
        ]);
// $couponIdDetail = $request->input('couponID');
        // Find the coupon by ID
        $coupon = CouponDiscountModel::findOrFail($id);

        
        $coupon->coupon_code = $request->input('coupon_code');
        $coupon->usage_limit = $request->input('usage_limit', $coupon->usage_limit);
        $coupon->apply_coupon_on_product = $request->input('apply_coupon_on_product', $coupon->apply_coupon_on_product);
        $coupon->discount = $request->input('discount', $coupon->discount);
        $coupon->orderValues = $request->input('orderValues', $coupon->orderValues);
        $coupon->discount_type = $request->input('discount_type', $coupon->discount_type);
        $coupon->orderQuality = $request->input('orderQuality', $coupon->orderQuality);
        $coupon->minimum_value = $request->input('minimum_value', $coupon->minimum_value);
        $coupon->upto_value = $request->input('upto_value', $coupon->upto_value);
        $coupon->start_date = $request->input('start_date', $coupon->start_date);
        $coupon->end_date = $request->input('end_date', $coupon->end_date);
        $coupon->time = $request->input('time', $coupon->time);
        $coupon->coupon_functionality = $request->input('coupon_functionality', $coupon->coupon_functionality);

        // Save the updated coupon
        $coupon->update();

        return response()->json(['message' => 'Coupon updated successfully!'], 200);
    } catch (\Illuminate\Database\QueryException $e) {
        // Handle database-related exceptions
        Log::error("Database error:", ['error' => $e->getMessage()]);
        return response()->json(['error' => 'Database error occurred'], 500);
    } catch (\Exception $e) {
        // Handle other exceptions
        Log::error("General error:", ['error' => $e->getMessage()]);
        return response()->json(['error' => 'Internal server error occurred'], 500);
    }
}
public function updateStatus(Request $request, $couponID)
    {
        // Find the coupon by its ID
        $coupon = CouponDiscountModel::find($couponID);

        if (!$coupon) {
            return response()->json(['error' => 'Coupon not found'], 404);
        }

        // Get the new status from the request
        $newStatus = $request->input('status');

        // Update the status in the database
        $coupon->status = $newStatus;
        $coupon->save();

        return response()->json(['message' => 'Status updated successfully']);
    }
public function allCoupons(Request $request){
    $allCoupon = CouponDiscountModel::all();
      return $allCoupon;

}
}
