<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Customer;
use App\Models\Expense;
use App\Models\Order;
use App\Models\Product;
use App\Models\Supplier;
use Illuminate\Http\Request;
use Illuminate\View\View;

class PosDataController extends Controller
{
    public function products(Request $request): View
    {
        $products = Product::with('user:id,name,email')
            ->when($request->q, fn($q) => $q->where(fn($sub) =>
                $sub->where('name', 'like', "%{$request->q}%")
                    ->orWhere('barcode', 'like', "%{$request->q}%")
                    ->orWhereHas('user', fn($u) => $u->where('email', 'like', "%{$request->q}%"))
            ))
            ->when($request->type, fn($q) => $q->where('type', $request->type))
            ->orderByDesc('updated_at')
            ->paginate(25)
            ->withQueryString();

        return view('admin.pos.products', compact('products'));
    }

    public function orders(Request $request): View
    {
        $orders = Order::with('user:id,name,email')
            ->when($request->q, fn($q) => $q->where(fn($sub) =>
                $sub->where('invoice_number', 'like', "%{$request->q}%")
                    ->orWhere('customer_name', 'like', "%{$request->q}%")
                    ->orWhereHas('user', fn($u) => $u->where('email', 'like', "%{$request->q}%"))
            ))
            ->when($request->status, fn($q) => $q->where('status', $request->status))
            ->orderByDesc('order_date')
            ->paginate(25)
            ->withQueryString();

        return view('admin.pos.orders', compact('orders'));
    }

    public function customers(Request $request): View
    {
        $customers = Customer::with('user:id,name,email')
            ->when($request->q, fn($q) => $q->where(fn($sub) =>
                $sub->where('name', 'like', "%{$request->q}%")
                    ->orWhere('phone', 'like', "%{$request->q}%")
                    ->orWhere('email', 'like', "%{$request->q}%")
                    ->orWhereHas('user', fn($u) => $u->where('email', 'like', "%{$request->q}%"))
            ))
            ->orderByDesc('updated_at')
            ->paginate(25)
            ->withQueryString();

        return view('admin.pos.customers', compact('customers'));
    }

    public function categories(Request $request): View
    {
        $categories = Category::with('user:id,name,email')
            ->when($request->q, fn($q) => $q->where(fn($sub) =>
                $sub->where('name', 'like', "%{$request->q}%")
                    ->orWhereHas('user', fn($u) => $u->where('email', 'like', "%{$request->q}%"))
            ))
            ->orderByDesc('updated_at')
            ->paginate(25)
            ->withQueryString();

        return view('admin.pos.categories', compact('categories'));
    }

    public function suppliers(Request $request): View
    {
        $suppliers = Supplier::with('user:id,name,email')
            ->when($request->q, fn($q) => $q->where(fn($sub) =>
                $sub->where('name', 'like', "%{$request->q}%")
                    ->orWhere('phone', 'like', "%{$request->q}%")
                    ->orWhere('email', 'like', "%{$request->q}%")
                    ->orWhereHas('user', fn($u) => $u->where('email', 'like', "%{$request->q}%"))
            ))
            ->orderByDesc('updated_at')
            ->paginate(25)
            ->withQueryString();

        return view('admin.pos.suppliers', compact('suppliers'));
    }

    public function expenses(Request $request): View
    {
        $expenses = Expense::with('user:id,name,email')
            ->when($request->q, fn($q) => $q->where(fn($sub) =>
                $sub->where('title', 'like', "%{$request->q}%")
                    ->orWhere('category_name', 'like', "%{$request->q}%")
                    ->orWhereHas('user', fn($u) => $u->where('email', 'like', "%{$request->q}%"))
            ))
            ->orderByDesc('expense_date')
            ->paginate(25)
            ->withQueryString();

        return view('admin.pos.expenses', compact('expenses'));
    }
}
