<?php

namespace App\Http\Controllers\Petani;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\Produk;
use App\Models\DetailTransaksi;

class DashboardController extends Controller
{
    public function index()
    {
        $petaniId = Auth::guard('petani')->id();

        // 
        $totalProduk = Produk::where('petani_id', $petaniId)->count();

        //  Hitung Pesanan Baru 
        $pesananBaru = DetailTransaksi::whereHas('produk', function($q) use ($petaniId) {
            $q->where('petani_id', $petaniId);
        })->whereHas('transaksi', function($q) {
            $q->where('status', 'dibayar'); 
        })->count();

        //  Hitung Total Pendapatan 
        $totalPendapatan = DetailTransaksi::whereHas('produk', function($q) use ($petaniId) {
            $q->where('petani_id', $petaniId);
        })->whereHas('transaksi', function($q) {
            $q->where('status', 'selesai');
        })->sum('subtotal');

        return view('petani.dashboard', compact('totalProduk', 'pesananBaru', 'totalPendapatan'));
    }
}