<?php

namespace App\Http\Controllers;

use App\Models\PaymentMode;
use Illuminate\Http\Request;

class PaymentModeController extends Controller
{
    /**
     * INDEX
     * Search + Pagination
     */
    public function index(Request $request)
    {
        try {
            $perPage = $request->per_page ?? 10;
            $search  = $request->search;

            // $query = PaymentMode::query();
            $query = PaymentMode::with('ledger');

            if (!empty($search)) {
                $query->where(function ($q) use ($search) {
                    $q->where('PayMd_Code', 'like', "%{$search}%")
                      ->orWhere('PayMd_Name', 'like', "%{$search}%")
                      ->orWhere('Is_Digital', 'like', "%{$search}%")
                      ->orWhere('Is_Cash', 'like', "%{$search}%")
                      ->orWhere('Is_Active', 'like', "%{$search}%");
                });
            }

            $data = $query->orderBy('PayMd_ID', 'desc')
                          ->paginate($perPage);

            return response()->json([
                'status'  => true,
                'message' => 'Payment Mode List',
                'data'    => $data
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'status'  => false,
                'message' => 'Something went wrong. Please try again later.'
            ]);
        }
    }

    /**
     * ALL DATA
     */


    public function allData()
    {
        try {
            // $data = PaymentMode::orderBy('PayMd_ID', 'desc')->get();

             $data = PaymentMode::with('ledger')
                ->orderBy('PayMd_ID', 'desc')
                ->get();


            return response()->json([
                'status'  => true,
                'message' => 'All Payment Mode Data',
                'count'   => $data->count(),
                'data'    => $data
            ]);

        } catch (\Exception $e) {
            return response()->json([
                'status'  => false,
                'message' => 'Something went wrong. Please try again later.'
            ]);
        }
    }

    /**
     * STORE
     */
    public function store(Request $request)
    {
        $request->validate([
            // 'PayMd_Code' => 'required|unique:payment_modes,PayMd_Code',
            'PayMd_Code' => [
            'nullable',
            \Illuminate\Validation\Rule::unique('payment_modes', 'PayMd_Code')
                ->whereNull('deleted_at')
        ],
            'PayMd_Name' => 'required',
            'Lg_Id'   => 'nullable|exists:ledger_master,Lg_Id',
            'Is_Digital' => 'nullable|boolean',
            'Is_Cash' => 'nullable|boolean',
            'Is_Active'  => 'nullable|boolean'
        ]);

        $data = PaymentMode::create([
            'PayMd_Code' => $request->PayMd_Code,
            'PayMd_Name' => $request->PayMd_Name,
            'Lg_Id'    => $request->Lg_Id,
            'Is_Digital' => $request->Is_Digital ?? 0,
            'Is_Cash' => $request->Is_Cash ?? 0,
            'Is_Active'  => $request->Is_Active ?? 1
        ]);

        return response()->json([
            'status'  => true,
            'message' => 'Created Successfully',
            'data'    => $data
        ]);
    }

    /**
     * SHOW SINGLE
     */
 public function show($id)
{
    $data = PaymentMode::with('ledger')->find($id);

    if (!$data) {
        return response()->json([
            'status'  => false,
            'message' => 'Record not found'
        ]);
    }

    return response()->json([
        'status' => true,
        'data'   => $data
    ]);
}

    /**
     * UPDATE
     * All fields nullable
     */

    public function update(Request $request, $id)
    {
        $data = PaymentMode::find($id);

        if (!$data) {
            return response()->json([
                'status'  => false,
                'message' => 'Record not found'
            ]);
        }

        $request->validate([
            // 'PayMd_Code' => 'nullable|unique:payment_modes,PayMd_Code,' . $id . ',PayMd_ID',
                'PayMd_Code' => [
                'nullable',
                \Illuminate\Validation\Rule::unique('payment_modes', 'PayMd_Code')
                    ->ignore($id, 'PayMd_ID')
                    ->whereNull('deleted_at')
            ],
            'PayMd_Name' => 'nullable',
            'Lg_Id'   => 'nullable|exists:ledger_master,Lg_Id',
            'Is_Digital' => 'nullable|boolean',
            'Is_Cash' => 'nullable|boolean',
            'Is_Active'  => 'nullable|boolean'
        ]);

        $data->update([
            'PayMd_Code' => $request->filled('PayMd_Code') ? $request->PayMd_Code : $data->PayMd_Code,
            'PayMd_Name' => $request->filled('PayMd_Name') ? $request->PayMd_Name : $data->PayMd_Name,
            'Lg_Id'   => $request->filled('Lg_Id') ? $request->Lg_Id : $data->Lg_Id,
            'Is_Digital' => $request->filled('Is_Digital') ? $request->Is_Digital : $data->Is_Digital,
            'Is_Cash' => $request->filled('Is_Cash') ? $request->Is_Cash : $data->Is_Cash,
            'Is_Active'  => $request->filled('Is_Active') ? $request->Is_Active : $data->Is_Active,
        ]);

        return response()->json([
            'status'  => true,
            'message' => 'Updated Successfully',
            'data'    => $data->fresh()
        ]);
    }

    /**
     * DELETE
     */



    public function destroy($id)
    {
        $data = PaymentMode::find($id);

        if (!$data) {
            return response()->json([
                'status'  => false,
                'message' => 'Record not found'
            ]);
        }

        $data->delete();

        return response()->json([
            'status'  => true,
            'message' => 'Deleted Successfully'
        ]);
    }


    public function nonDigitalNonCash()
{
    try {

        $data = PaymentMode::with('ledger')
            ->where('Is_Digital', 0)
            ->where('Is_Cash', 0)
            ->orderBy('PayMd_ID', 'desc')
            ->get();

        return response()->json([
            'status'  => true,
            'message' => 'Non Digital & Non Cash Payment Modes',
            'count'   => $data->count(),
            'data'    => $data
        ]);

    } catch (\Exception $e) {

        return response()->json([
            'status'  => false,
            'message' => config('app.debug')
                ? $e->getMessage()
                : 'Something went wrong. Please try again later.'
        ], 500);
    }
}

}
