<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Models\User;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests\ProfileUpdateRequest;
use App\Models\Transaksi;

class ProfileController extends Controller
{
    /**
     * Display the user's profile form.
     */
    public function index(Request $request): View
    {
        // Mendapatkan tanggal hari ini
        // $today = Carbon::now()->toDateString();
        $user = Auth::user();
        if ($user->role == 'bendahara-excellent')
            $jurusan = 'excellent';
        else
            $jurusan = 'reguler';
        // Mengambil transaksi terbaru pada tanggal hari ini
        $trans = Transaksi::with(['user', 'jenistagihan'])
            ->where('jurusan', $jurusan)
            ->where('status', '1')
            ->whereNotNull('tgl_pembayaran')
            ->latest()
            ->get();
        $totaltransaksi = Transaksi::where('status', '1')->count();
        $trans->map(function ($item) {
            $item->tgl_pembayaran_formatted = \Carbon\Carbon::parse($item->tgl_pembayaran)->format('F j, Y');
            return $item;
        });
        return view('pages.edit-profile', compact('user', 'trans', 'totaltransaksi'));
    }

    /**
     * Update the user's profile information.
     */
    public function update(Request $request, $id)
    {
        try {
            $item = User::findOrFail($id);
            $data = $request->all();
            if ($request->hasFile('foto')) {
                // Jika ada file yang diunggah, simpan file baru dan gunakan path yang baru
                $data['foto'] = $request->file('foto')->store('assets/foto', 'public');
            } else {
                // Jika tidak ada file yang diunggah, gunakan foto lama (path yang sudah ada)
                $data['foto'] = $item->foto;
            }


            $item->update($data);

            return redirect()->route('profile.index')->with('success', 'Data berhasil diperbarui.');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
        }
    }


    /**
     * Delete the user's account.
     */
    public function destroy(Request $request): RedirectResponse
    {
        $request->validateWithBag('userDeletion', [
            'password' => ['required', 'current_password'],
        ]);

        $user = $request->user();

        Auth::logout();

        $user->delete();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return Redirect::to('/');
    }
}
