<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\PengajuanMasjid;
use App\Models\Masjid;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class SuperAdminController extends Controller
{
    /**
     * Tampilkan Dashboard Super Admin
     */
    public function dashboard()
    {
        $totalMasjid = Masjid::count();
        $menungguVerifikasi = PengajuanMasjid::where('status_pengajuan', 'pending')->count();
        $masjidAktif = Masjid::count(); // Assuming all registered are active, or add logic if needed
        $pengajuanDitolak = PengajuanMasjid::where('status_pengajuan', 'ditolak')->count();

        $pengajuanTerbaru = PengajuanMasjid::orderBy('created_at', 'desc')->take(4)->get();

        return view('superadmin.dashboard', compact(
            'totalMasjid', 
            'menungguVerifikasi', 
            'masjidAktif', 
            'pengajuanDitolak', 
            'pengajuanTerbaru'
        ));
    }

    /**
     * Tampilkan Halaman Kelola Pengajuan
     */
    public function kelolaPengajuan(Request $request)
    {
        $query = PengajuanMasjid::query();

        // Filter berdasarkan status
        if ($request->filled('status') && $request->status !== 'Semua Status') {
            $query->where('status_pengajuan', strtolower($request->status));
        }

        // Pencarian berdasarkan nama masjid
        if ($request->filled('search')) {
            $query->where('nama_masjid', 'like', '%' . $request->search . '%');
        }

        $pengajuan = $query->orderBy('created_at', 'desc')->paginate(15);
        $pengajuan->appends($request->all());

        return view('superadmin.pengajuan_masjid', compact('pengajuan'));
    }

    /**
     * Tampilkan Halaman Kelola Masjid (Masjid yang sudah terdaftar)
     */
    public function kelolaMasjid(Request $request)
    {
        $query = Masjid::query();

        if ($request->filled('search')) {
            $query->where('nama_masjid', 'like', '%' . $request->search . '%');
        }

        $masjid = $query->orderBy('created_at', 'desc')->paginate(15);
        $masjid->appends($request->all());

        return view('superadmin.kelola_masjid', compact('masjid'));
    }

    /**
     * Tambah Masjid Manual oleh Super Admin
     */
    public function storeMasjid(Request $request)
    {
        $request->validate([
            'nama_masjid'   => 'required|string|max:255',
            'alamat_masjid' => 'required|string',
            'nama_admin'    => 'required|string|max:255',
            'username'      => 'required|string|max:50|unique:users,username',
            'email'         => 'required|email|unique:users,email',
            'password'      => 'required|string|min:6|confirmed',
            'latitude'      => 'nullable|numeric|between:-90,90',
            'longitude'     => 'nullable|numeric|between:-180,180',
        ], [
            'email.unique'       => 'Email ini sudah terdaftar oleh pengguna lain.',
            'password.confirmed' => 'Konfirmasi password tidak cocok.',
        ]);

        $masjid = Masjid::create([
            'nama_masjid'   => $request->nama_masjid,
            'alamat_masjid' => $request->alamat_masjid,
            'tahun_aktif'   => now()->year,
            'latitude'      => $request->latitude ?: null,
            'longitude'     => $request->longitude ?: null,
        ]);

        User::create([
            'nama_user'    => $request->nama_admin,
            'username'     => $request->username,
            'email'        => $request->email,
            'password'     => Hash::make($request->password),
            'int_role'     => 'admin_masjid',
            'id_masjid'    => $masjid->id_masjid,
            'status_aktif' => true,
        ]);

        return redirect()->back()->with('success', 'Masjid dan Admin Masjid berhasil ditambahkan!');
    }

    /**
     * Tampilkan Modal Edit Masjid (data JSON untuk JS atau redirect)
     */
    public function editMasjid($id)
    {
        $masjid = Masjid::findOrFail($id);
        return response()->json($masjid);
    }

    /**
     * Update data Masjid (nama, alamat, koordinat)
     */
    public function updateMasjid(Request $request, $id)
    {
        $masjid = Masjid::findOrFail($id);

        $request->validate([
            'nama_masjid'   => 'required|string|max:255',
            'alamat_masjid' => 'required|string',
            'latitude'      => 'nullable|numeric|between:-90,90',
            'longitude'     => 'nullable|numeric|between:-180,180',
        ]);

        $masjid->update([
            'nama_masjid'   => $request->nama_masjid,
            'alamat_masjid' => $request->alamat_masjid,
            'latitude'      => $request->latitude ?: null,
            'longitude'     => $request->longitude ?: null,
        ]);

        return redirect()->back()->with('success', 'Data masjid berhasil diperbarui!');
    }
}
