<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\LokasiTps;
use App\Models\KategoriTps;
use Illuminate\Support\Facades\Storage;

class TpsController extends Controller
{
    public function index()
    {
        $title = 'Data TPS';
        $tps = LokasiTps::with('kategori')
            ->withCount('aduan')
            ->get();

        return view('admin.tps.index', compact('title', 'tps'));
    }

    public function create()
    {
        $title = 'Tambah TPS';
        $kategori = KategoriTps::all();

        return view('admin.tps.create', compact('title', 'kategori'));
    }

    /*
    |--------------------------------------------------------------------------
    | KONVERSI KOORDINAT (DECIMAL / DMS)
    |--------------------------------------------------------------------------
    */
    private function convertToDecimal($coordinate)
    {
        if (is_numeric($coordinate)) {
            return (float) $coordinate;
        }

        $coordinate = html_entity_decode($coordinate);
        $coordinate = strtoupper(trim($coordinate));
        $coordinate = str_replace(['°', "'", '"'], [' ', ' ', ' '], $coordinate);

        preg_match('/([NSEW])/', $coordinate, $dir);
        if (!$dir) {
            return null;
        }

        preg_match_all('/\d+(\.\d+)?/', $coordinate, $numbers);
        if (count($numbers[0]) < 3) {
            return null;
        }

        [$deg, $min, $sec] = array_map('floatval', $numbers[0]);
        $decimal = $deg + ($min / 60) + ($sec / 3600);

        if (in_array($dir[1], ['S', 'W'])) {
            $decimal *= -1;
        }

        return $decimal;
    }

    /*
    |--------------------------------------------------------------------------
    | STORE (CREATE)
    |--------------------------------------------------------------------------
    */
    public function store(Request $request)
    {
        // VALIDASI DASAR + PESAN CUSTOM
        $request->validate(
            [
                'kategori_tps_id' => 'required|exists:kategori_tps,id_kategori_tps',
                'nama_tps'        => 'required|string|max:255',
                'alamat_tps'      => 'required|string|max:255',
                'status_tps'      => 'required|in:Aktif,Tidak Aktif,Pembangunan',
                'tahun_pembuatan' => 'required|digits:4',
                'kapasitas_tps'   => 'required|integer|min:1',
                'latitude'        => 'required',
                'longitude'       => 'required',
                'foto_tps'        => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
            ],
            [
                'kategori_tps_id.required' => 'Kategori TPS wajib dipilih.',
                'kategori_tps_id.exists'   => 'Kategori TPS tidak valid.',

                'nama_tps.required'   => 'Nama TPS wajib diisi.',
                'alamat_tps.required' => 'Alamat TPS wajib diisi.',

                'status_tps.required' => 'Status TPS wajib dipilih.',
                'status_tps.in'       => 'Status TPS tidak valid.',

                'tahun_pembuatan.required' => 'Tahun pembuatan wajib diisi.',
                'tahun_pembuatan.digits'   => 'Tahun pembuatan harus 4 digit (contoh: 2022).',

                'kapasitas_tps.required' => 'Kapasitas TPS wajib diisi.',
                'kapasitas_tps.integer'  => 'Kapasitas TPS harus berupa angka.',
                'kapasitas_tps.min'      => 'Kapasitas TPS minimal 1.',

                'latitude.required'  => 'Latitude wajib diisi.',
                'longitude.required' => 'Longitude wajib diisi.',

                'foto_tps.image' => 'Foto TPS harus berupa gambar.',
                'foto_tps.mimes' => 'Format foto TPS harus jpg, jpeg, atau png.',
            ]
        );

        // VALIDASI KOORDINAT (TIDAK DOBEL ERROR)
        $latitude  = $this->convertToDecimal($request->latitude);
        $longitude = $this->convertToDecimal($request->longitude);

        $errors = [];

        if ($latitude === null || $latitude < -90 || $latitude > 90) {
            $errors['latitude'] = 'Latitude tidak valid. Contoh: -7.623 atau 7.623';
        }

        if ($longitude === null || $longitude < -180 || $longitude > 180) {
            $errors['longitude'] = 'Longitude tidak valid. Contoh: 111.980 atau -111.980';
        }

        if (!empty($errors)) {
            return back()->withErrors($errors)->withInput();
        }

        // UPLOAD FOTO
        $foto = $request->hasFile('foto_tps')
            ? $request->file('foto_tps')->store('foto-tps', 'public')
            : null;

        LokasiTps::create([
            'kategori_tps_id' => $request->kategori_tps_id,
            'nama_tps'        => $request->nama_tps,
            'alamat_tps'      => $request->alamat_tps,
            'status_tps'      => $request->status_tps,
            'tahun_pembuatan' => $request->tahun_pembuatan,
            'kapasitas_tps'   => $request->kapasitas_tps,
            'latitude'        => $latitude,
            'longitude'       => $longitude,
            'foto_tps'        => $foto,
        ]);

        return redirect()->route('admin.tps.index')
            ->with('success', 'Data TPS berhasil ditambahkan.');
    }

    /*
    |--------------------------------------------------------------------------
    | EDIT
    |--------------------------------------------------------------------------
    */
    public function edit($id)
    {
        $title = 'Edit TPS';
        $tps = LokasiTps::findOrFail($id);
        $kategori = KategoriTps::all();

        return view('admin.tps.edit', compact('title', 'tps', 'kategori'));
    }

    /*
    |--------------------------------------------------------------------------
    | UPDATE
    |--------------------------------------------------------------------------
    */
    public function update(Request $request, $id)
    {
        $tps = LokasiTps::findOrFail($id);

        $request->validate(
            [
                'kategori_tps_id' => 'required|exists:kategori_tps,id_kategori_tps',
                'nama_tps'        => 'required|string|max:255',
                'alamat_tps'      => 'required|string|max:255',
                'status_tps'      => 'required|in:Aktif,Tidak Aktif,Pembangunan',
                'tahun_pembuatan' => 'required|digits:4',
                'kapasitas_tps'   => 'required|integer|min:1',
                'latitude'        => 'required',
                'longitude'       => 'required',
                'foto_tps'        => 'nullable|image|mimes:jpg,jpeg,png|max:4096',
            ],
            [
                'tahun_pembuatan.digits' => 'Tahun pembuatan harus 4 digit.',
            ]
        );

        $latitude  = $this->convertToDecimal($request->latitude);
        $longitude = $this->convertToDecimal($request->longitude);

        $errors = [];

        if ($latitude === null || $latitude < -90 || $latitude > 90) {
            $errors['latitude'] = 'Latitude tidak valid.';
        }

        if ($longitude === null || $longitude < -180 || $longitude > 180) {
            $errors['longitude'] = 'Longitude tidak valid.';
        }

        if (!empty($errors)) {
            return back()->withErrors($errors)->withInput();
        }

        if ($request->hasFile('foto_tps')) {
            if ($tps->foto_tps) {
                Storage::disk('public')->delete($tps->foto_tps);
            }
            $foto = $request->file('foto_tps')->store('foto-tps', 'public');
        } else {
            $foto = $tps->foto_tps;
        }

        $tps->update([
            'kategori_tps_id' => $request->kategori_tps_id,
            'nama_tps'        => $request->nama_tps,
            'alamat_tps'      => $request->alamat_tps,
            'status_tps'      => $request->status_tps,
            'tahun_pembuatan' => $request->tahun_pembuatan,
            'kapasitas_tps'   => $request->kapasitas_tps,
            'latitude'        => $latitude,
            'longitude'       => $longitude,
            'foto_tps'        => $foto,
        ]);

        return redirect()->route('admin.tps.index')
            ->with('success', 'Data TPS berhasil diperbarui.');
    }

    /*
    |--------------------------------------------------------------------------
    | DELETE
    |--------------------------------------------------------------------------
    */
    public function destroy($id)
    {
        $tps = LokasiTps::findOrFail($id);

        if ($tps->foto_tps) {
            Storage::disk('public')->delete($tps->foto_tps);
        }

        $tps->delete();

        return redirect()->route('admin.tps.index')
            ->with('success', 'Data TPS berhasil dihapus.');
    }
}
