<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\NotificationLog;
use App\Models\PurchasedPlan;
use App\Models\PurchasedPlaylist;
use App\Models\Transaction;
use App\Models\UserLogin;
use Illuminate\Http\Request;

class ReportController extends Controller
{
    public function transaction(Request $request,$userId = null)
    {
        $pageTitle = 'Transaction Logs';

        $remarks = Transaction::distinct('remark')->orderBy('remark')->get('remark');

        $transactions = Transaction::searchable(['trx','user:username'])->filter(['trx_type','remark'])->dateFilter()->orderBy('id','desc')->with('user');
        if ($userId) {
            $transactions = $transactions->where('user_id',$userId);
        }
        $transactions = $transactions->paginate(getPaginate());

        return view('admin.reports.transactions', compact('pageTitle', 'transactions','remarks'));
    }

    public function playlistPurchasedHistory(Request $request)
    {
        $pageTitle = 'Playlist Purchased History';
        $purchasedPlaylists = PurchasedPlaylist::orderBy('id','desc')->with('user', 'owner', 'playlist')->searchable(['user:username', 'owner:username', 'playlist:title'])->dateFilter()->paginate(getPaginate());
        return view('admin.reports.playlists', compact('pageTitle', 'purchasedPlaylists'));
    }

    public function planPurchasedHistory(Request $request)
    {
        $pageTitle = 'Plan Purchased History';
        $purchasedPlans  = PurchasedPlan::orderBy('id','desc')->with('user', 'owner', 'plan')->searchable(['user:username', 'owner:username', 'plan:name'])->dateFilter()->paginate(getPaginate());
        return view('admin.reports.plans', compact('pageTitle', 'purchasedPlans'));
    }

    public function loginHistory(Request $request)
    {
        $pageTitle = 'User Login History';
        $loginLogs = UserLogin::orderBy('id','desc')->searchable(['user:username'])->dateFilter()->with('user')->paginate(getPaginate());
        return view('admin.reports.logins', compact('pageTitle', 'loginLogs'));
    }

    public function loginIpHistory($ip)
    {
        $pageTitle = 'Login by - ' . $ip;
        $loginLogs = UserLogin::where('user_ip',$ip)->orderBy('id','desc')->with('user')->paginate(getPaginate());
        return view('admin.reports.logins', compact('pageTitle', 'loginLogs','ip'));
    }

    public function notificationHistory(Request $request){
        $pageTitle = 'Notification History';
        $logs = NotificationLog::orderBy('id','desc')->searchable(['user:username'])->dateFilter()->with('user')->paginate(getPaginate());
        return view('admin.reports.notification_history', compact('pageTitle','logs'));
    }

    public function emailDetails($id){
        $pageTitle = 'Email Details';
        $email = NotificationLog::findOrFail($id);
        return view('admin.reports.email_details', compact('pageTitle','email'));
    }
}
