<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ActionMaster;
use Exception;
use Illuminate\Support\Facades\DB;

class ActionMasterController extends Controller
{

    
    public function index(Request $request)
    {
        try {

            $query = ActionMaster::with('previousStatus', 'previousStatus.department', 'department');
            if ($request->search) {

                $search = $request->search;

                $query->where(function ($q) use ($search) {

                    $q->where('St_Name', 'LIKE', "%$search%")
                        ->orWhere('stage', 'LIKE', "%$search%")
                        ->orWhere('Act_Remarks', 'LIKE', "%$search%");
                });
            }

            $data = $query->orderBy('Act_Id', 'desc')->paginate(10);

            return response()->json([
                'status' => true,
                'message' => 'Action list fetched',
                'data' => $data
            ]);
        } catch (Exception $e) {

            return response()->json([
                'status' => false,
                'message' => 'Something went wrong',
               
            ], 500);
        }
    }



    public function all(Request $request)
    {
        try {

            $query = ActionMaster::with('previousStatus', 'previousStatus.department', 'department');
            if ($request->filled('stage')) {
                $query->where('stage', $request->stage);
            }


            $data = $query->orderBy('Act_Id', 'desc')->get();

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

            return response()->json([
                'status' => false,
                'message' => 'Something went wrong. Please try again later.',
                
            ], 500);
        }
    }



    public function show($id)
    {
        try {

            $data = ActionMaster::with('previousStatus', 'previousStatus.department', 'department')->find($id);
            if (!$data) {
                return response()->json([
                    'status' => false,
                    'message' => 'Record not found'
                ], 404);
            }

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

            return response()->json([
                'status' => false,
                'message' => 'Something went wrong. Please try again later.',
                
            ], 500);
        }
    }



    public function store(Request $request)
    {
        try {

            $request->validate([

                'Dpt_id' => 'nullable|exists:dept_master,Dpt_id',
                'St_Name' => 'nullable|max:25',
                'Previous_Status' => 'nullable|exists:action_master,Act_Id',
                'stage' => 'nullable|max:5',
                'Is_Forward' => 'nullable|in:Y,N',
                'Is_Bank_Select' => 'nullable|in:Y,N',
                'Is_Acc_Vou_No_Gen' => 'nullable|in:Y,N',
                'Viewers' => 'nullable',
                'Authorities' => 'nullable',
                'Act_Remarks' => 'nullable|max:150'

            ]);


            $data = ActionMaster::create([

                'Dpt_id' => $request->Dpt_id,
                'St_Name' => $request->St_Name,
                'Previous_Status' => $request->Previous_Status,
                'stage' => $request->stage,
                'Viewers' => json_encode($request->Viewers),
                'Authorities' => json_encode($request->Authorities),
                'Act_Remarks' => $request->Act_Remarks,
                'Is_Forward' => $request->Is_Forward ?? 'Y',
                'Is_Bank_Select' => $request->Is_Bank_Select ?? 'N',
                'Is_Acc_Vou_No_Gen' => $request->Is_Acc_Vou_No_Gen ?? 'N'

            ]);

            return response()->json([
                'status' => true,
                'message' => 'Record created successfully',
                'data' => $data
            ]);
        } catch (Exception $e) {

            return response()->json([
                'status' => false,
                'message' => 'Something went wrong. Please try again later.',
                
            ], 500);
        }
    }



    public function update(Request $request, $id)
    {
        try {

            $data = ActionMaster::find($id);

            if (!$data) {
                return response()->json([
                    'status' => false,
                    'message' => 'Something went wrong. Please try again later.',
                ], 404);
            }


            $request->validate([

                'Dpt_id' => 'nullable|exists:dept_master,Dpt_id',
                'St_Name' => 'sometimes|nullable',
                'Previous_Status' => 'sometimes|nullable|exists:action_master,Act_Id',
                'stage' => 'sometimes|nullable',
                'Viewers' => 'sometimes|nullable',
                'Authorities' => 'sometimes|nullable',
                'Act_Remarks' => 'sometimes|nullable',
                'Is_Forward' => 'sometimes|in:Y,N',
                'Is_Bank_Select' => 'sometimes|in:Y,N',
                'Is_Acc_Vou_No_Gen' => 'sometimes|in:Y,N',

            ]);


            $data->update([

                'Dpt_id' => $request->Dpt_id ?? $data->Dpt_id,
                'St_Name' => $request->St_Name ?? $data->St_Name,
                'Previous_Status' => $request->Previous_Status ?? $data->Previous_Status,
                'stage' => $request->stage ?? $data->stage,
                'Viewers' => $request->Viewers ? json_encode($request->Viewers) : $data->Viewers,
                'Authorities' => $request->Authorities ? json_encode($request->Authorities) : $data->Authorities,
                'Act_Remarks' => $request->Act_Remarks ?? $data->Act_Remarks,
                'Is_Forward' => $request->Is_Forward ?? $data->Is_Forward,
                'Is_Bank_Select' => $request->Is_Bank_Select ?? $data->Is_Bank_Select,
                'Is_Acc_Vou_No_Gen' => $request->Is_Acc_Vou_No_Gen ?? $data->Is_Acc_Vou_No_Gen

            ]);

            return response()->json([
                'status' => true,
                'message' => 'Record updated successfully',
                'data' => $data
            ]);
        } catch (Exception $e) {

            return response()->json([
                'status' => false,
                'message' => 'Something went wrong. Please try again later.',
                
            ], 500);
        }
    }



    public function destroy($id)
{
    try {

        $data = ActionMaster::find($id);

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

        
        $isUsed = DB::table('chl_voucher')
            ->where('status', $id)
            ->exists();

        if ($isUsed) {
            return response()->json([
                'status' => false,
                'message' => 'This Action is used in Challan Voucher, so it cannot be deleted.'
            ], 400);
        }

        $data->delete();

        return response()->json([
            'status' => true,
            'message' => 'Record deleted successfully'
        ]);

    } catch (Exception $e) {

        return response()->json([
            'status' => false,
            'message' => 'Failed to delete record',
            'error' => env('Development_Mode') == 'Prod' 
        ? 'Something went wrong. Please try again later.' 
        : $e->getMessage()
        ], 500);
    }
}

}
