<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use App\Models\KandangAyam;
use Carbon\Carbon;
use DB;

class DashboardController extends Controller
{
    public function index(Request $request)
    {
        $userId = Auth::id(); 
        $totalKandangAyams = KandangAyam::where('status_kandang', 'Aktif')
                                        ->where('user_id', $userId) 
                                        ->count();

        $totalCapacity = KandangAyam::where('status_kandang', 'Aktif')
                                    ->where('user_id', $userId) 
                                    ->sum('kapasitas');

        $totalDeathsThisMonth = DB::table('harian_ayam')
            ->whereMonth('tanggal_input', Carbon::now()->month)
            ->whereYear('tanggal_input', Carbon::now()->year)
            ->whereIn('id_populasi', function ($query) use ($userId) {
                $query->select('id')->from('populasi_ayam')->where('user_id', $userId);
            })
            ->sum('jumlah_ayam_mati');

        $currentMonthDeaths = DB::table('harian_ayam')
            ->whereYear('tanggal_input', Carbon::now()->year)
            ->whereMonth('tanggal_input', Carbon::now()->month)
            ->whereIn('id_populasi', function ($query) use ($userId) {
                $query->select('id')->from('populasi_ayam')->where('user_id', $userId);
            })
            ->sum('jumlah_ayam_mati');

        $previousMonthDeaths = DB::table('harian_ayam')
            ->whereYear('tanggal_input', Carbon::now()->year)
            ->whereMonth('tanggal_input', Carbon::now()->subMonth()->month)
            ->whereIn('id_populasi', function ($query) use ($userId) {
                $query->select('id')->from('populasi_ayam')->where('user_id', $userId);
            })
            ->sum('jumlah_ayam_mati');

        $percentageChange = 0;
        if ($previousMonthDeaths > 0) {
            $percentageChange = (($currentMonthDeaths - $previousMonthDeaths) / $previousMonthDeaths) * 100;
        }

        $populasiSub = DB::table('populasi_ayam')
            ->whereIn('status_ayam', ['Proses', 'Siap Panen'])
            ->where('user_id', $userId)  
            ->select('kandang_id', DB::raw('SUM(jumlah_ayam_masuk) as total_ayam'))
            ->groupBy('kandang_id');

        $harianSub = DB::table('harian_ayam')
            ->join('populasi_ayam', 'harian_ayam.id_populasi', '=', 'populasi_ayam.id')
            ->whereIn('populasi_ayam.status_ayam', ['Proses', 'Siap Panen'])
            ->where('populasi_ayam.user_id', $userId)  
            ->select('populasi_ayam.kandang_id', 
                    DB::raw('SUM(jumlah_ayam_sakit) as total_sick'),
                    DB::raw('SUM(jumlah_ayam_mati) as total_dead'))
            ->groupBy('populasi_ayam.kandang_id');
        $KandangAyams = DB::table('kandang_ayam')
            ->where('user_id', $userId)  
            ->leftJoinSub($populasiSub, 'populasi', function ($join) {
                $join->on('kandang_ayam.id', '=', 'populasi.kandang_id');
            })
            ->leftJoinSub($harianSub, 'harian', function ($join) {
                $join->on('kandang_ayam.id', '=', 'harian.kandang_id');
            })
            ->select(
                'kandang_ayam.*',
                DB::raw('COALESCE(populasi.total_ayam, 0) as total_ayam'),
                DB::raw('COALESCE(harian.total_sick, 0) as total_sick'),
                DB::raw('COALESCE(harian.total_dead, 0) as total_dead')
            )
            ->having('total_ayam', '>', 0)
            ->paginate(5);

        $pendapatanBulanIni = DB::table('pendapatan')
            ->whereMonth('tanggal_transaksi', Carbon::now()->month)
            ->whereYear('tanggal_transaksi', Carbon::now()->year)
            ->where('user_id', $userId)  
            ->sum('total_pendapatan');

        $pengeluaranBulanIni = DB::table('pengeluaran')
            ->whereMonth('tanggal_pembelian', Carbon::now()->month)
            ->whereYear('tanggal_pembelian', Carbon::now()->year)
            ->where('user_id', $userId) 
            ->sum('total_biaya');

        return view('dashboard', compact(
            'totalKandangAyams',
            'totalCapacity',
            'totalDeathsThisMonth',
            'percentageChange',
            'KandangAyams',
            'pendapatanBulanIni',
            'pengeluaranBulanIni',
        ));
    }
}
