<?php

namespace App\Http\Controllers;

use App\Models\Booking;
use App\Models\Payement;
use App\Models\User;
use App\Models\Installment;
use App\Models\Account;
use App\Models\Refund;
use App\Models\Ticket;
use App\Models\Transaction;
use App\Models\Office;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\BannerReportExport;



class AdminController extends Controller
{
    public function dashboard()
    {
        $from = Carbon::parse(sprintf(
            '%s 00:00',
            request()->query('from', Carbon::now()->toDateString())
        ));
        $to = Carbon::parse(sprintf(
            '%s 23:59:59',
            request()->query('to', Carbon::now()->toDateString())
        ));
        
        $ticket_used =Transaction::whereHas('ticket', function($q){
                $q->where('ticket_status', '=', 'U');
            })->whereBetween('created_at', [$from, $to])->sum('amount');

        $ticket_open =Transaction::whereHas('ticket', function($q){
                $q->where('ticket_status', '=', 'O');
            })->whereBetween('created_at', [$from, $to])->sum('amount');
        
        $office_balance =Account::whereBetween('created_at', [$from, $to])->sum('balance');
  
        $transfer_success =Installment::whereHas('transfer.transaction', function($q)use($from,$to){
            
          $q->where('status', '=', 'success')->whereBetween('created_at', [$from, $to]);

            
            })->sum('amount');
        $transfer_pending =Transaction::where('status', '=', 'pending')->whereBetween('created_at', [$from, $to])->sum('amount');
        
        $refund_amount =Refund::sum('amount');
        

        $open_reservation_count = Booking::where('status', Booking::‫‪STATE_OPEN‬‬)->count();
        $pending_payement_count = Payement::where('status', Payement::STATUS_PENDING)->count();
        $users_count = User::where('admin_id',auth()->id())->count();
        return view(
            'dashboard',
            compact('open_reservation_count', 'pending_payement_count', 'users_count','ticket_used','ticket_open','transfer_success','transfer_pending','office_balance','refund_amount')
        );
    }

    public function notif(){
        return auth()->user()->unreadNotifications;
    }

    public function markNotif(Request $request){
        auth()->user()
            ->unreadNotifications
            ->when($request->input('id'),function($query) use($request){
                return $query->where('id', $request->input('id'));
            })
            ->markAsRead();
    }

    public function profile()
    {
        return view('profile', ['admin' => auth()->guard('web')->user()]);
    }

    public function update(Request $request)
    {
        $user = auth()->user();

        $data = $this->validate($request, [
            'name' => 'required|string',
            'email' => 'required|email',
            'image' => 'image'
        ]);
        if ($request->image) {
            $data['image'] = time() . '.' . $request->image->extension();
            $request->image->move(public_path('uploads'), $data['image']);
            $this->destroyImage($user->image);
        }
        if ($request->password) {
            $data['password'] = bcrypt($request->password);
        }
        unset(Auth::user()->currency_code);
        auth()->user()->update($data);
        return redirect()->route('user.profile')->with('success', __('Task was successful'));  
    }

    public function destroyImage($name)
    {
        $image_path = public_path() . '/uploads/' . $name;
        return @unlink($image_path);
    }
    public function bannerExport(){
        
         $office = Office::find(auth()->user()->office_id);
        $name = $office?$office->name:auth()->user()->name;
        $name .= '-'.request()->query('from').'-'.request()->query('to');
        return Excel::download(new BannerReportExport, $name.'.xlsx');
        
     }
}
