<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Transactions;
use App\Models\Transaction;
use App\Models\Refund;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

class AdminDashboardController extends Controller
{
    public function index()
    {
        $transactions = Transactions::allTransactions();
        $currentMonth = Carbon::now()->month;
        $currentYear = Carbon::now()->year;

        $countSuccess = Transaction::where('status', 'settlement')
            ->whereMonth('updated_at', $currentMonth)
            ->whereYear('updated_at', $currentYear)
            ->count();

        $countPending = Transaction::where('status', 'pending')
            ->whereMonth('updated_at', $currentMonth)
            ->whereYear('updated_at', $currentYear)
            ->count();

        $countCancelled = Transaction::where('status', 'cancel')
            ->whereMonth('updated_at', $currentMonth)
            ->whereYear('updated_at', $currentYear)
            ->count();

        $countRefund = Transaction::where('status', 'refund')
            ->whereMonth('updated_at', $currentMonth)
            ->whereYear('updated_at', $currentYear)
            ->count();

        $totalTransaction = Transaction::whereMonth('updated_at', $currentMonth)
            ->whereYear('updated_at', $currentYear)
            ->count();

        $dataChartTransaction = [];
        $dataChartRefund = [];

        $totalRefund = Transaction::where('status', 'refund')->count();
        $dataChartTotalRefund = [];

        $totalUser = User::where('status', 'Finished')->count();
        $dataChartTotalUser = [];

        for ($bulan = 1; $bulan <= 12; $bulan++) {
            $transaction = Transaction::whereMonth('updated_at', $bulan)
                ->whereYear('updated_at', $currentYear)
                ->where('status', 'settlement')
                ->sum('total_bayar');

            $refund = Transaction::whereMonth('updated_at', $bulan)
                ->whereYear('updated_at', $currentYear)
                ->where('status', 'partial refund')
                ->sum('total_harga');

            $dataChartTransaction[] = intval($transaction);
            $dataChartRefund[] = intval($refund);
        }

        return view('admin.index', compact('transactions', 'countSuccess', 'countPending', 'countCancelled', 'countRefund', 'totalRefund', 'totalUser', 'totalTransaction', 'dataChartTransaction', 'dataChartRefund', 'dataChartTotalUser', 'dataChartTotalRefund'));
    }

    public function getChartByMonth()
    {
        $dataChartLaporan = [];
        $tahun = Carbon::now()->year;
    }

    public function getCharByYear()
    {
    }
}