<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Location;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;

class LocationController extends Controller
{
    public function index(Request $request)
    {
        $data = Location::query();
        $search = $request->input('search.value', '');
        if (!empty($search)) {
            $data->where('name', 'like', '%' . $search . '%');
        }
        $total_data = $data->count();
        $length = intval($request->input('length', 0));
        $start = intval($request->input('start', 0));
        $data = $data->orderBy('created_at', 'desc');
        if (!$length && !$start) {
            $data = $data->get();
        } else {
            $data = $data->skip($start)->take($length)->get();
        }
        return response()->json([
            'message' => 'Data berhasil diambil.',
            'data' => $data,
            'draw' => $request->input('draw'),
            'recordsTotal' => $total_data,
            'recordsFiltered' => $total_data,
        ], 200);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string',
            'center_lat' => ['required', 'numeric', 'regex:/^-?\d{1,3}\.\d{4,}$/'],
            'center_lng' => ['required', 'numeric', 'regex:/^-?\d{1,3}\.\d{4,}$/'],
            'radius' => 'required|integer|min:1',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'message' => Str::ucfirst($validator->errors()->first()),
                'data' => null
            ], 422);
        }
        $location = Location::create($request->only(['name', 'center_lat', 'center_lng', 'radius']));
        return response()->json([
            'message' => 'Lokasi berhasil ditambahkan.',
            'data' => $location
        ], 200);
    }

    public function show(string $name)
    {
        $location = Location::where('name', $name)->first();
        if (!$location) {
            return response()->json([
                'message' => 'Data tidak ditemukan.',
                'data' => null
            ], 422);
        }
        return response()->json([
            'message' => 'Data berhasil diambil.',
            'data' => $location
        ], 200);
    }

    public function update(Request $request, string $name)
    {
        $location = Location::where('name', $name)->first();
        if (!$location) {
            return response()->json([
                'message' => 'Data tidak ditemukan.',
                'data' => null
            ], 422);
        }
        $validator = Validator::make($request->all(), [
            'center_lat' => ['required', 'numeric', 'regex:/^-?\d{1,3}\.\d{4,}$/'],
            'center_lng' => ['required', 'numeric', 'regex:/^-?\d{1,3}\.\d{4,}$/'],
            'radius' => 'required|integer|min:1',
        ]);
        if ($validator->fails()) {
            return response()->json([
                'message' => Str::ucfirst($validator->errors()->first()),
                'data' => null
            ], 422);
        }
        $location->update($request->only(['center_lat', 'center_lng', 'radius']));
        return response()->json([
            'message' => 'Lokasi berhasil diubah.',
            'data' => $location
        ], 200);
    }

    public function destroy(string $name)
    {
        $location = Location::where('name', $name)->first();
        if (!$location) {
            return response()->json([
                'message' => 'Data tidak ditemukan.',
                'data' => null
            ], 422);
        }
        $location->delete();
        return response()->json([
            'message' => 'Lokasi berhasil dihapus.',
            'data' => null
        ], 200);
    }
}
