<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\LinkHistory;
use App\Models\FaucetHistory;
use App\Models\PtcHistory;
use App\Models\Deposit;
use App\Models\WithdrawHistory;
use App\Models\OfferwallHistory;
use App\Models\ContestHistory;
use App\Models\ReferralCommission;
use Illuminate\Support\Facades\DB;

use Carbon\Carbon;

class ChartController extends Controller
{
    public function loadChart(Request $request)
    {
        $endDate = Carbon::parse($request->query('end_date', now()->toDateString()))->addDay();
        $startDate = Carbon::parse($request->query('start_date', now()->subMonth()->toDateString()));

        $eventType = $request->query('event_type', 'all');
        $data = [];

        switch ($eventType) {
            case 'Faucet':
                $data = FaucetHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;

            case 'PTC Ads':
                $data = PtcHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;

            case 'Shortlink':
                $data = LinkHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;

            case 'Contest':
                $data = ContestHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;

            case 'Offerwalls':
                $data = OfferwallHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->where('status', 1)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'Referrals':
                $data = ReferralCommission::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amount) as total_reward')
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'Deposit':
                $data = Deposit::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amount) as total_deposit')
                    ->where('status', 'completed')
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'Withdraw':
                $data = WithdrawHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amountusd) as total_withdraw, SUM(fee) as total_fee')
                    ->where('status', 1)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'all':
                $data = DB::table(function ($query) use ($startDate, $endDate) {
                    $query->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                        ->from('link_history')
                        ->whereBetween('created_at', [$startDate, $endDate])
                        ->groupBy('date')
                        ->unionAll(
                            DB::table('faucet_history')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('ptc_history')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('offerwall_history')
                                ->where('status', 1)
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('contest_history')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('referral_commissions')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amount) as total_reward')
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        );
                }, 'combined')
                    ->select('date', DB::raw('SUM(count) as count, SUM(total_reward) as total_reward'))
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
        }

        return response()->json($data);
    }
    public function loadChartUser(Request $request, $userId)
    {
        $endDate = Carbon::parse($request->query('end_date', now()->toDateString()))->addDay();
        $startDate = Carbon::parse($request->query('start_date', now()->subMonth()->toDateString()));

        $eventType = $request->query('event_type', 'all');
        $data = [];

        switch ($eventType) {
            case 'Faucet':
                $data = FaucetHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->where('user_id', $userId)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;

            case 'PTC Ads':
                $data = PtcHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->where('user_id', $userId)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;

            case 'Shortlink':
                $data = LinkHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->where('user_id', $userId)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'Contest':
                $data = ContestHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->where('user_id', $userId)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;

            case 'Offerwalls':
                $data = OfferwallHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                    ->where('user_id', $userId)
                    ->where('status', 1)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'Referrals':
                $data = ReferralCommission::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amount) as total_reward')
                    ->where('referrer_id', $userId)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'Deposit':
                $data = Deposit::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amount) as total_deposit')
                    ->where('user_id', $userId)
                    ->where('status', 'completed')
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'Withdraw':
                $data = WithdrawHistory::selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amountusd) as total_withdraw, SUM(fee) as total_fee')
                    ->where('user_id', $userId)
                    ->where('status', 1)
                    ->whereBetween('created_at', [$startDate, $endDate])
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
                
            case 'all':
                $data = DB::table(function ($query) use ($startDate, $endDate, $userId) {
                    $query->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                        ->from('link_history')
                        ->where('user_id', $userId)
                        ->whereBetween('created_at', [$startDate, $endDate])
                        ->groupBy('date')
                        ->unionAll(
                            DB::table('faucet_history')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->where('user_id', $userId)
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('ptc_history')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->where('user_id', $userId)
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('contest_history')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->where('user_id', $userId)
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('offerwall_history')
                                ->where('status', 1)
                                ->where('user_id', $userId)
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(reward) as total_reward')
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        )
                        ->unionAll(
                            DB::table('referral_commissions')
                                ->selectRaw('DATE(created_at) as date, COUNT(*) as count, SUM(amount) as total_reward')
                                ->where('referrer_id', $userId)
                                ->whereBetween('created_at', [$startDate, $endDate])
                                ->groupBy('date')
                        );
                }, 'combined')
                    ->select('date', DB::raw('SUM(count) as count, SUM(total_reward) as total_reward'))
                    ->groupBy('date')
                    ->orderBy('date', 'ASC')
                    ->get();
                break;
        }

        return response()->json($data);
    }
}
