<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\PdOrder;
use Carbon\Carbon;

class ResellerDashboardController extends Controller
{
    public function __invoke(Request $request)
    {
        $user = $request->user();

        // টাইমজোন (প্রয়োজনে .env / config এ Asia/Dhaka সেট করুন)
        $tz  = config('app.timezone', 'Asia/Dhaka');
        $now = Carbon::now($tz);

        // Day windows (রাত 12:00 -> রাত 11:59:59)
        $todayStart     = $now->copy()->startOfDay();
        $todayEnd       = $now->copy()->endOfDay();

        $yesterdayStart = $todayStart->copy()->subDay();
        $yesterdayEnd   = $todayEnd->copy()->subDay();

        // শেষ ৭ দিন (আজ সহ)
        $last7Start     = $now->copy()->subDays(6)->startOfDay();
        // শেষ ৩০ দিন (আজ সহ)
        $last30Start    = $now->copy()->subDays(29)->startOfDay();

        $base = PdOrder::where('user_id', $user->id);

        // ===== Earnings (সব স্ট্যাটাসে মোট লাভ) =====
        $totalEarn      = (clone $base)->sum('earn_total');

        $earnToday      = (clone $base)
                            ->whereBetween('created_at', [$todayStart, $todayEnd])
                            ->sum('earn_total');

        $earnYesterday  = (clone $base)
                            ->whereBetween('created_at', [$yesterdayStart, $yesterdayEnd])
                            ->sum('earn_total');

        $earnLast7      = (clone $base)
                            ->where('created_at', '>=', $last7Start)
                            ->sum('earn_total');

        $earnLast30     = (clone $base)
                            ->where('created_at', '>=', $last30Start)
                            ->sum('earn_total');

        // ===== Orders count =====
        $ordersToday    = (clone $base)
                            ->whereBetween('created_at', [$todayStart, $todayEnd])
                            ->count();

        $ordersYesterday= (clone $base)
                            ->whereBetween('created_at', [$yesterdayStart, $yesterdayEnd])
                            ->count();

        $ordersLast7    = (clone $base)
                            ->where('created_at', '>=', $last7Start)
                            ->count();

        $ordersLast30   = (clone $base)
                            ->where('created_at', '>=', $last30Start)
                            ->count();

        // ===== Status summary =====
        $statusCounts = [
            'pending'    => (clone $base)->where('status','pending')->count(),
            'processing' => (clone $base)->where('status','processing')->count(),
            'rejected'   => (clone $base)->where('status','rejected')->count(),
            'completed'  => (clone $base)->where('status','completed')->count(),
        ];

        // চাইলে শুধুমাত্র completed অর্ডার দিয়ে ইনকাম সামারি করতে পারেন:
        // $baseCompleted = (clone $base)->where('status','completed');
        // তারপর উপরের earnToday/earnLast7 ইত্যাদি $baseCompleted ব্যবহার করে ক্যালকুলেট করুন।

        return view('user.reseller.dashboard', [
            'now'            => $now,
            'totalEarn'      => $totalEarn,
            'earn'           => [
                'today'     => $earnToday,
                'yesterday' => $earnYesterday,
                'last7'     => $earnLast7,
                'last30'    => $earnLast30,
            ],
            'orders'         => [
                'today'     => $ordersToday,
                'yesterday' => $ordersYesterday,
                'last7'     => $ordersLast7,
                'last30'    => $ordersLast30,
            ],
            'statusCounts'   => $statusCounts,
        ]);
    }
}