<?php

namespace App\Http\Controllers\Admin;

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

class ManageLocationController extends Controller
{

    /*
    Created By : Sayali parab
    Created at : 07 June 2024
    Use : To get the page.
    */
    public function index()
    {
        $location = ManageState::orderBy('id', 'asc')->get();
        return view('Admin.pages.manage_states.manage_states', compact('location'));
    }
    /*
    Created By : Sayali parab
    Created at : 07 June 2024
    Use : To store the location.
    */


    public function store(Request $request)
    {
        $request->validate([
            'location_name' => 'required|string|max:255'
        ]);

        try {
            if (ManageState::where('name', $request->location_name)->exists()) {
                return response()->json(['success' => false, 'error' => 'Location name already exists', 'status' => 422]);
            }


            $location = new ManageState();
            $location->name = $request->location_name;
            $location->save();

            return response()->json(['success' => true, 'status' => 200]);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'error' => $e->getMessage(), 'status' => 500]);
        }
    }


    /*
    Created By : Sayali parab
    Created at : 07 June 2024
    Use : To change status.
    */
    public function change_location_status(Request $request)
    {

        try {
            DB::beginTransaction();
            $status = ManageState::find($request->location_id);
            $status->is_active = $request->status;
            $status->save();
            // return response()->json(['status' => 200]);
            DB::commit();

            return jsonResponseWithSuccessMessage(__('success.update_data'));
        } catch (Exception $e) {
            Log::error("Update Status function Load Failed " . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }


    /*
    Created By : Sayali parab
    Created at : 07 June 2024
    Use : To delete location.
    */
    public function delete_location($id)
    {


        try {
            DB::beginTransaction();

            $passport = ManageState::find($id);
            $passport->delete();

            DB::commit();

            return response()->json(['success' => true, 'status' => 200]);
        } catch (Exception $e) {
            DB::rollBack();
            Log::error("delete_location function Load Failed " . $e->getMessage());
            return response()->json(['success' => false, 'status' => 500, 'message' => __('auth.something_went_wrong')]);
        }
    }


    /*
    Created By : Sayali parab
    Created at : 07 June 2024
    Use : To update location.
    */
    public function update(Request $request)
    {


        try {
            DB::beginTransaction();

            $location_data = ManageState::find($request->id);
            $location_data->name = $request->location_name;
            $location_data->save();

            DB::commit();

            return jsonResponseWithSuccessMessage(__('success.update_data'));
        } catch (Exception $e) {
            DB::rollBack();
            Log::error("Failed to update location: " . $e->getMessage());
            return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
        }
    }
}
