<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AduanTps;
use App\Models\LokasiTps;

class AduanController extends Controller
{
    /**
     * Menampilkan halaman form aduan TPS
     */
    public function index(Request $request)
    {
        $title = 'Aduan TPS';
        $tps = null;

        if ($request->filled('tps_id')) {
            $tps = LokasiTps::with('kategori')
                ->where('id_tps', $request->tps_id)
                ->first();
        }

        $listTps = LokasiTps::orderBy('nama_tps')->get();

        return view('user.aduan-tps', compact(
            'title',
            'tps',
            'listTps'
        ));
    }

    /**
     * Menyimpan data aduan TPS
     */
    public function store(Request $request)
    {
        $request->validate([
            'lokasi_tps_id'   => 'required|exists:lokasi_tps,id_tps',
            'nama_pelapor'    => 'required|string|max:100',
            'alamat_pelapor'  => 'required|string',
            'no_pelapor'      => 'required|digits_between:1,13|regex:/^[0-9]+$/',
            'isi_aduan'       => 'required|string',
            'bukti_foto'      => 'required|image|mimes:jpg,jpeg,png|max:2048',
        ], [
            'lokasi_tps_id.required'   => 'TPS wajib dipilih',
            'lokasi_tps_id.exists'     => 'TPS tidak valid',

            'nama_pelapor.required'    => 'Nama pelapor wajib diisi',
            'alamat_pelapor.required'  => 'Alamat pelapor wajib diisi',

            'no_pelapor.required'      => 'Nomor HP wajib diisi',
            'no_pelapor.digits_between'=> 'Nomor HP maksimal 13 angka',
            'no_pelapor.regex'         => 'Nomor HP hanya boleh berisi angka',

            'isi_aduan.required'       => 'Isi aduan wajib diisi',

            'bukti_foto.required'      => 'Foto bukti wajib diunggah',
            'bukti_foto.image'         => 'File harus berupa gambar',
            'bukti_foto.mimes'         => 'Format gambar harus jpg, jpeg, atau png',
            'bukti_foto.max'           => 'Ukuran gambar maksimal 2 MB',
        ]);

        // Upload foto bukti ke folder publik langsung
        $fotoPath = null;
        if ($request->hasFile('bukti_foto')) {
            $file = $request->file('bukti_foto');
            $filename = strtolower(str_replace(' ', '_', pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)))
                        . '_' . time() . '.' . $file->getClientOriginalExtension();
            $file->move(public_path('assets/user/img/aduan'), $filename);
            $fotoPath = $filename;
        }

        // Simpan ke database
        AduanTps::create([
            'lokasi_tps_id' => $request->lokasi_tps_id,
            'nama_pelapor'  => $request->nama_pelapor,
            'alamat_pelapor'=> $request->alamat_pelapor,
            'no_pelapor'    => $request->no_pelapor,
            'isi_aduan'     => $request->isi_aduan,
            'bukti_foto'    => $fotoPath,
            'tanggal_aduan' => now(),
        ]);

        return redirect()
            ->route('user.aduan')
            ->with('success', 'Aduan berhasil dikirim');
    }
}
