<?php

namespace App\Http\Controllers;

use App\Models\DataKK;
use App\Models\DataMustahik;
use App\Models\PenerimaanZakat;
use App\Models\User;
use App\Models\Wilayah;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Carbon;

class DataKKController extends Controller
{
    /**
     * Dashboard Admin Masjid: Menampilkan statistik real
     * Aktor: Admin Masjid
     */
    public function dashboard()
    {
        $masjidId = Auth::user()->id_masjid;

        // Statistik utama
        $totalAmil             = User::where('id_masjid', $masjidId)->where('int_role', 'amil')->count();
        $totalWilayah          = Wilayah::where('id_masjid', $masjidId)->count();
        $totalMustahikDisetujui = DataMustahik::where('id_masjid', $masjidId)->where('status', 'Disetujui')->count();
        $totalZakatBulanIni    = PenerimaanZakat::where('id_masjid', $masjidId)
                                    ->whereMonth('tanggal_terima', Carbon::now()->month)
                                    ->whereYear('tanggal_terima', Carbon::now()->year)
                                    ->sum('total_bayar');

        // Data chart 6 bulan terakhir
        $hijriMonths = ['Muharram','Safar','Rabiul Awal','Rabiul Akhir','Jumadil Awal','Jumadil Akhir',
                        'Rajab','Syaban','Ramadan','Syawal','Zulkaidah','Zulhijjah'];
        $masehiMonths = ['Jan','Feb','Mar','Apr','Mei','Jun','Jul','Agu','Sep','Okt','Nov','Des'];

        $chartLabels = [];
        $chartData   = [];
        for ($i = 5; $i >= 0; $i--) {
            $date  = Carbon::now()->subMonths($i);
            $total = PenerimaanZakat::where('id_masjid', $masjidId)
                        ->whereMonth('tanggal_terima', $date->month)
                        ->whereYear('tanggal_terima', $date->year)
                        ->sum('total_bayar');
            $chartLabels[] = $masehiMonths[$date->month - 1] . ' / ' . $hijriMonths[($date->month + 6) % 12];
            $chartData[]   = (float) $total;
        }

        return view('admin.dashboard', compact(
            'totalAmil', 'totalWilayah', 'totalMustahikDisetujui',
            'totalZakatBulanIni', 'chartLabels', 'chartData'
        ));
    }

    /**
     * Menampilkan daftar data KK / warga
     * Aktor: Admin Masjid
     */
    public function index()
    {
        // Ambil data KK yang terikat dengan masjid yang sedang login
        $masjidId = Auth::user()->id_masjid;
        
        $masyarakat = DataKK::where('id_masjid', $masjidId)->with('wilayah')->get();

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

    /**
     * Menampilkan form tambah data KK baru
     * Aktor: Admin Masjid
     */
    public function create()
    {
        $masjidId = Auth::user()->id_masjid;
        // Ambil data wilayah RT/RW hanya yang ada di masjid tersebut
        $wilayah = Wilayah::where('id_masjid', $masjidId)->get();
        
        return view('admin.masyarakat.create', compact('wilayah'));
    }

    /**
     * Menyimpan data KK baru ke database
     * Aktor: Admin Masjid
     */
    public function store(Request $request)
    {
        $request->validate([
            'nik_kepala_kk' => 'required|unique:DataKK,nik_kepala_kk',
            'nama_kepala_kk' => 'required',
            'alamat_rumah' => 'required',
            'id_wilayah' => 'required',
            'jumlah_tanggungan' => 'required|numeric',
        ]);

        DataKK::create([
            'nik_kepala_kk' => $request->nik_kepala_kk,
            'nama_kepala_kk' => $request->nama_kepala_kk,
            'alamat_rumah' => $request->alamat_rumah,
            'id_wilayah' => $request->id_wilayah,
            'jumlah_tanggungan' => $request->jumlah_tanggungan,
            'id_masjid' => Auth::user()->id_masjid
        ]);

        return redirect('/admin/masyarakat')->with('success', 'Data KK warga berhasil ditambahkan!');
    }
}
