<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

use Carbon\Carbon;
use App\Models\User;
use App\Models\AdminDetails;
use App\Models\Companies;
use App\Models\Supliers;
use App\Models\Categories;
use App\Models\Brands;
use App\Models\Products;
use App\Models\ProductGallery;
use App\Models\GRNS;
use App\Models\Returns;
use App\Models\GRNProducts;
use App\Models\Orders;
use App\Models\CashierDetails;
use App\Models\Stock;
use App\Models\RevenuesAndExpenses;
use App\Models\Riders;
use App\Models\Waiters;
use App\Models\Shifts;

use File;
use Mail;
use Image;
use PDF;
class ReportController extends Controller
{
    public function cost_report(Request $request)
    { if($request->isMethod('get')){
        $products = Products::where('status','active')->get();
        return view('admin::report.cost_report',['products' => $products]);
    }
   
    }
     public function financial_reports(Request $request)
    { if($request->isMethod('get')){
        $products = Products::where('status','active')->get();
        return view('admin::report.financial_reports',['products' => $products]);
    }
   
    }
     public function daily_updates(Request $request)
    { if($request->isMethod('get')){
         $customers = User::where('user_role','2')->with('customerDetails')->get();
        return view('admin::report.daily_updates',['customers' => $customers]);
    }
   
    }
   public function inventory_reports(Request $request)
    { if($request->isMethod('get')){
        $products = Products::where('status', 'active')->get();
        return view('admin::report.inventory_reports',['products' => $products]);
    }
   
    }
    public function profit_loss_summery(Request $request)
{
    $startDate = $request->query('start_date');
    $endDate = $request->query('end_date');

    $ordersQuery = Orders::orderBy('id', 'asc')->where('order_status', 'complete');

    $revenuesAndExpensesQuery = RevenuesAndExpenses::selectRaw('expense_category, MIN(id) as id, MIN(date) as date, MIN(type) as type')
        ->groupBy('expense_category')
        ->orderBy('date', 'asc');

    if ($startDate && $endDate) {
        $ordersQuery->whereBetween('created_at', [
            $startDate . ' 00:00:00',
            $endDate . ' 23:59:59'
        ]);

        $revenuesAndExpensesQuery->whereBetween('date', [
            $startDate . ' 00:00:00',
            $endDate . ' 23:59:59'
        ]);
    }

    $orders = $ordersQuery->get();

    
    $revenues_and_expenses = $revenuesAndExpensesQuery->get();

    return view('admin::report.profit_loss_summery', [
        'orders' => $orders,
        'revenues_and_expenses' => $revenues_and_expenses,
        'start_date' => $startDate,
        'end_date' => $endDate
    ]);
}

    public function profit_loss_detailed(Request $request)
{
    $startDate = $request->query('start_date');
    $endDate = $request->query('end_date');

    $ordersQuery = Orders::orderBy('id', 'asc')->where('order_status', 'complete');
    $revenuesAndExpensesQuery = RevenuesAndExpenses::orderBy('date', 'asc');

    if ($startDate && $endDate) {
        // Format date range to include full end day
        $ordersQuery->whereBetween('created_at', [
            $startDate . ' 00:00:00',
            $endDate . ' 23:59:59'
        ]);

        $revenuesAndExpensesQuery->whereBetween('date', [
            $startDate . ' 00:00:00',
            $endDate . ' 23:59:59'
        ]);
    }

    $orders = $ordersQuery->get();
    $revenues_and_expenses = $revenuesAndExpensesQuery->get();

    return view('admin::report.profit_loss_detailed', [
        'orders' => $orders,
        'revenues_and_expenses' => $revenues_and_expenses,
        'start_date' => $startDate,
        'end_date' => $endDate
    ]);
}



public function sales_report(Request $request)
{
    $query = Orders::orderBy('id', 'desc')->where('order_status', 'complete');
    $cashiers = CashierDetails::orderBy('id', 'desc')->get();
    $customers = User::where('user_role','2')->with('customerDetails')->get();
    $waiters = Waiters::where('status','active')->get();
    $riders = Riders::where('status','active')->get();

    if ($request->has('start_date') && $request->start_date) {
        $query->where('created_at', '>=', Carbon::parse($request->start_date));
    }
    
    if ($request->has('end_date') && $request->end_date) {
        $query->where('created_at', '<=', Carbon::parse($request->end_date));
    }

    if ($request->has('cashier') && $request->cashier != 'all') {
        $query->where('sales_person', $request->cashier);
    }

    if ($request->has('invoice_type') && $request->invoice_type != 'all') {
        $query->where('order_type', $request->invoice_type);
    }

    if ($request->has('customer') && $request->customer != 'all') {
        $query->where('customer', $request->customer);
    }

    if ($request->has('waiter') && $request->waiter != 'all') {
        $query->where('waiter', $request->waiter);
    }

    if ($request->has('rider') && $request->rider != 'all') {
        $query->where('rider', $request->rider);
    }

    $orders = $query->get();

    return view('admin::report.sales_reports', [
        'orders' => $orders,
        'cashiers' => $cashiers,
        'customers' => $customers,
        'waiters' => $waiters,
        'riders' => $riders
    ]);
}
 
public function shift_report(Request $request)
{
    $query = Shifts::orderBy('id', 'desc');
    $cashiers = CashierDetails::orderBy('id', 'desc')->get();


    if ($request->has('start_date') && $request->start_date) {
        $query->where('date_time', '>=', Carbon::parse($request->start_date));
    }
    
    if ($request->has('end_date') && $request->end_date) {
        $query->where('date_time', '<=', Carbon::parse($request->end_date));
    }

    if ($request->has('cashier') && $request->cashier != 'all') {
        $query->where('user_id', $request->cashier);
    }


    $shifts = $query->get();

    return view('admin::report.shift_report', [
        'shifts' => $shifts,
        'cashiers' => $cashiers,
    ]);
}
}