<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Models\OfficeLocation;
use Illuminate\Http\Request;

class OfficeLocationController extends Controller
{
    public function index()
    {
        $locations = OfficeLocation::withCount('attendances')->latest()->get();
        return view('admin.office_locations.index', compact('locations'));
    }

    public function store(Request $request)
    {
        if ($request->has('latitude')) {
            $request->merge(['latitude' => str_replace([' ', ','], ['', '.'], trim((string)$request->input('latitude')))]);
        }
        if ($request->has('longitude')) {
            $request->merge(['longitude' => str_replace([' ', ','], ['', '.'], trim((string)$request->input('longitude')))]);
        }

        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'address' => ['nullable', 'string'],
            'latitude' => ['required', 'numeric', 'between:-90,90'],
            'longitude' => ['required', 'numeric', 'between:-180,180'],
            'radius_meters' => ['required', 'integer', 'min:1', 'max:10000'],
            'max_gps_accuracy' => ['nullable', 'integer', 'min:5', 'max:500'],
            'is_active' => ['boolean'],
        ]);

        $validated['is_active'] = $request->boolean('is_active', true);
        $validated['max_gps_accuracy'] = $validated['max_gps_accuracy'] ?? 50;

        $location = OfficeLocation::create($validated);
        AuditLog::log('Tambah Lokasi Kantor', "Menambahkan lokasi kantor: {$location->name} (Radius: {$location->radius_meters}m, Max Akurasi: {$location->max_gps_accuracy}m)");

        return redirect()->route('admin.office-locations.index')->with('success', 'Lokasi kantor baru berhasil ditambahkan.');
    }

    public function update(Request $request, OfficeLocation $officeLocation)
    {
        if ($request->has('latitude')) {
            $request->merge(['latitude' => str_replace([' ', ','], ['', '.'], trim((string)$request->input('latitude')))]);
        }
        if ($request->has('longitude')) {
            $request->merge(['longitude' => str_replace([' ', ','], ['', '.'], trim((string)$request->input('longitude')))]);
        }

        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'address' => ['nullable', 'string'],
            'latitude' => ['required', 'numeric', 'between:-90,90'],
            'longitude' => ['required', 'numeric', 'between:-180,180'],
            'radius_meters' => ['required', 'integer', 'min:1', 'max:10000'],
            'max_gps_accuracy' => ['nullable', 'integer', 'min:5', 'max:500'],
            'is_active' => ['boolean'],
        ]);

        $validated['is_active'] = $request->boolean('is_active', true);
        $validated['max_gps_accuracy'] = $validated['max_gps_accuracy'] ?? 50;

        $officeLocation->update($validated);
        AuditLog::log('Edit Lokasi Kantor', "Memperbarui lokasi kantor: {$officeLocation->name} (Radius: {$officeLocation->radius_meters}m, Max Akurasi: {$officeLocation->max_gps_accuracy}m)");

        return redirect()->route('admin.office-locations.index')->with('success', 'Lokasi kantor berhasil diperbarui.');
    }

    public function destroy(OfficeLocation $officeLocation)
    {
        $name = $officeLocation->name;
        $officeLocation->delete();
        AuditLog::log('Hapus Lokasi Kantor', "Menghapus lokasi kantor: {$name}");

        return redirect()->route('admin.office-locations.index')->with('success', 'Lokasi kantor berhasil dihapus.');
    }
}
