<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Iuran;
use App\Models\Penghuni;
use App\Models\Rumah;
use App\Models\Layanan;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;

class IuranController extends Controller
{
    public function index()
    {
        $user = Auth::user();

        if ($user->role == 'admin') {
            $iuran = Iuran::with('penghuni')->get();
        } else {
            $iuran = Iuran::with('penghuni')
                ->where('rt_id', $user->id)
                ->get();
        }

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

    public function create()
    {
        $rtId = Auth::id();

        $penghuni = Penghuni::where('rt_id', $rtId)->get();

        return view('admin.iuran.create', compact('penghuni'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'penghuni_id' => 'required',
            'bulan' => 'required',
            'tahun' => 'required',
            'jumlah' => 'required',
            'jenis_iuran' => 'required',
            'keterangan' => 'required',
        ]);

        // 🔥 ambil penghuni
        $penghuni = Penghuni::findOrFail($request->penghuni_id);

        Iuran::create([
            'penghuni_id' => $request->penghuni_id,

            // 🔥 FIX UTAMA (ambil dari penghuni)
            'rt_id' => $penghuni->rt_id,

            'bulan' => $request->bulan,
            'tahun' => $request->tahun,
            'jumlah' => $request->jumlah,
            'jenis_iuran' => $request->jenis_iuran,
            'keterangan' => $request->keterangan,
            'status' => 'belum',
        ]);

         // 🔥 WHATSAPP
        $penghuni = Penghuni::find($request->penghuni_id);

        if ($penghuni && $penghuni->telepon) {
            $nohp = preg_replace('/^0/', '62', $penghuni->telepon);

            $pesan = "Halo {$penghuni->nama},\n";
            $pesan .= "Iuran baru ditambahkan:\n";
            $pesan .= "Bulan: {$request->bulan} {$request->tahun}\n";
            $pesan .= "Jumlah: Rp " . number_format($request->jumlah,0,',','.') . "\n";
            $pesan .= "Jenis: {$request->jenis_iuran}\n";
            $pesan .= "Terima kasih 🙏";

            Http::withHeaders([
                'Authorization' => env('FONNTE_TOKEN')
            ])->post('https://api.fonnte.com/send', [
                'target' => $nohp,
                'message' => $pesan,
            ]);
        }

        return redirect()->route('iuran.index')
            ->with('success', 'Data iuran berhasil ditambahkan');
    }
    public function edit($id)
    {
        $iuran = Iuran::where('rt_id', Auth::id())->findOrFail($id);
        $penghuni = Penghuni::where('rt_id', Auth::id())->get();

        return view('admin.iuran.edit', compact('iuran', 'penghuni'));
    }

    public function update(Request $request, $id)
    {
        $iuran = Iuran::where('rt_id', Auth::id())->findOrFail($id);

        $request->validate([
            'penghuni_id' => 'required',
            'bulan' => 'required',
            'tahun' => 'required',
            'jumlah' => 'required',
            'jenis_iuran' => 'required',
            'keterangan' => 'required',
            'status' => 'required',
        ]);

        $iuran->update([
            'penghuni_id' => $request->penghuni_id,
            'bulan' => $request->bulan,
            'tahun' => $request->tahun,
            'jumlah' => $request->jumlah,
            'jenis_iuran' => $request->jenis_iuran,
            'keterangan' => $request->keterangan,
            'status' => $request->status,
            'tanggal_bayar' => $request->status == 'lunas' ? now() : null,
        ]);

        return redirect()->route('iuran.index')->with('success', 'Data berhasil diupdate');
    }

    public function destroy($id)
    {
        $iuran = Iuran::where('rt_id', Auth::id())->findOrFail($id);
        $iuran->delete();

        return back()->with('success', 'Data berhasil dihapus');
    }

    public function dashboardRT()
    {
        $rtId = Auth::id();

       $totalPenghuni = Penghuni::count();
        $totalRumah = Rumah::count();
        $totalIuran = Iuran::count();
        $totalPengaduan = Layanan::count();

        $menungguRT = Iuran::where('rt_id', $rtId)
            ->where('status', 'belum')
            ->count();

        $menungguAdmin = Iuran::where('rt_id', $rtId)
            ->where('status', 'proses')
            ->count();

        return view('rt.dashboard', compact(
            'totalPenghuni',
            'totalRumah',
            'totalIuran',
            'totalPengaduan',
            'menungguRT',
            'menungguAdmin'
        ));
    }

        public function approveRT($id)
    {
        $iuran = Iuran::where('rt_id', Auth::id())->findOrFail($id);

        $iuran->update([
            'status' => 'menunggu',
            'tanggal_bayar' => now()
        ]);

        return back()->with('success', 'Dikirim ke Admin');
    }

        public function approveAdmin($id)
    {
        $iuran = Iuran::findOrFail($id);

        $iuran->update([
            'status' => 'lunas',
            'tanggal_bayar' => now()
        ]);

        return back()->with('success', 'Iuran disetujui admin');
    }
}
