<?php

namespace App\Http\Controllers\Admin\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\Barbershop;
use App\Models\Expense;
use App\Models\Payroll;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class OverviewController extends Controller
{
    public function index()
    {
        $todayIncome = Transaction::query()
            ->barbershop()
            ->whereDate('created_at', now()->today())
            ->when(auth()->user()->role_id === 5, function ($query) {
                return $query->where('user_id', auth()->id());
            })
            ->sum('total');
        $yesterdayIncome = Transaction::query()
            ->barbershop()
            ->whereDate('created_at', now()->yesterday())
            ->when(auth()->user()->role_id === 5, function ($query) {
                return $query->where('user_id', auth()->id());
            })
            ->sum('total');

        $todayShaving = Transaction::query()
            ->barbershop()
            ->today()
            ->when(auth()->user()->role_id === 5, function ($query) {
                return $query->where('user_id', auth()->id());
            })
            ->count();
        $yesterdayShaving = Transaction::query()
            ->barbershop()
            ->yesterday()
            ->when(auth()->user()->role_id === 5, function ($query) {
                return $query->where('user_id', auth()->id());
            })
            ->count();

        $todayExpense = Expense::query()->barbershop()->today()->sum('amount');
        $yesterdayExpense = Expense::query()->barbershop()->yesterday()->sum('amount');
        $todayPayroll = Payroll::query()->barbershop()->today()->sum('total');
        $yesterdayPayroll = Payroll::query()->barbershop()->yesterday()->sum('amount');

        $todayNewCustomer = Transaction::with('customer')->whereDoesntHave('customer.transactions', function ($query) {
            $query->where('id', '<>', DB::raw('transactions.id'));
        })
            ->barbershop()
            ->today()
            ->distinct('customer_id')
            ->count('customer_id');

        $yesterdayNewCustomer = Transaction::with('customer')->whereDoesntHave('customer.transactions', function ($query) {
            $query->where('id', '<>', DB::raw('transactions.id'));
        })
            ->barbershop()
            ->yesterday()
            ->distinct('customer_id')
            ->count('customer_id');

        $topCustomersByTotal = Transaction::groupBy('customer_id')
            ->select(
                'customer_id',
                DB::raw('SUM(total) as total_transaction'),
                DB::raw('COUNT(*) as transaction_count')
            )
            ->orderBy('total_transaction', 'desc')
            ->when(auth()->user()->role_id === 5, function ($query) {
                return $query->where('user_id', auth()->id());
            })
            ->limit(5)
            ->get();

        $incomeByBarber = User::with('biography')
            ->barbershop()
            ->barber()
            ->withSum('transactions', 'total')
            ->withCount('transactions')
            ->get();

        return view('pages.admin.dashboard.overview', [
            'pageTitle' => 'Ringkasan',
            'todayIncome' => $todayIncome,
            'yesterdayIncome' => $yesterdayIncome,
            'todayShaving' => $todayShaving,
            'yesterdayShaving' => $yesterdayShaving,
            'todayExpense' => $todayExpense + $todayPayroll,
            'yesterdayExpense' => $yesterdayExpense + $yesterdayPayroll,
            'todayNewCustomer' => $todayNewCustomer,
            'yesterdayNewCustomer' => $yesterdayNewCustomer,
            'cash' => Barbershop::findOrFail(Auth::user()->barbershop_id)->cash,
            'topCustomersByTotal' => $topCustomersByTotal,
            'incomeByBarber' => $incomeByBarber,
        ]);
    }

    public function changeBarbershop(Barbershop $barbershop): RedirectResponse
    {
        $user = User::findOrFail(Auth::id());
        $user->barbershop_id = $barbershop->id;
        $user->save();
        notify()->success('Barbershop berhasil diubah', 'Berhasil');

        return redirect()->route('dashboard.overview');
    }
}
