<?php

namespace App\Http\Controllers;

use App\Models\Account;
use App\Models\ShopProduct;
use App\Models\ShopProductPrice;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;
use Yajra\DataTables\Facades\DataTables;

class ShopProductController extends Controller
{
    public function index(): View
    {
        // Get all shops for filter dropdown
        $shops = Account::where('model_type', 'shop')
            ->where('is_active', true)
            ->orderBy('name')
            ->get(['id', 'name']);

        // Get all accounts (companies) that have shop products
        $companies = Account::whereHas('shopProducts')
            ->orderBy('name')
            ->get(['id', 'name']);

        return view('shop.products.index', compact('shops', 'companies'));
    }

    public function getData(Request $request): JsonResponse
    {
        $query = ShopProductPrice::with(['shopProduct.account', 'shop', 'shopProduct.stocks']);

        // Apply filters
        if ($request->filled('shop_id')) {
            $query->where('shop_id', $request->shop_id);
        }

        if ($request->filled('company_id')) {
            $query->whereHas('shopProduct', function ($q) use ($request) {
                $q->where('account_id', $request->company_id);
            });
        }

        if ($request->filled('search')) {
            $search = $request->search;
            $query->whereHas('shopProduct', function ($q) use ($search) {
                $q->where('name', 'like', "%{$search}%")
                  ->orWhere('shop_product_sku', 'like', "%{$search}%")
                  ->orWhere('code', 'like', "%{$search}%");
            });
        }

        return DataTables::of($query)
            ->addColumn('product_name', function ($price) {
                return $price->shopProduct->name ?? '';
            })
            ->addColumn('company_name', function ($price) {
                return $price->shopProduct->account->name ?? '';
            })
            ->addColumn('shop_name', function ($price) {
                return $price->shop->name ?? '';
            })
            ->addColumn('sku', function ($price) {
                return $price->shopProduct->shop_product_sku ?? '';
            })
            ->addColumn('barcode', function ($price) {
                return $price->shopProduct->shop_product_barcode ?? '';
            })
            ->addColumn('selling_price', function ($price) {
                return 'Rs. ' . number_format($price->selling_price, 2);
            })
            ->addColumn('cost_price', function ($price) {
                return $price->cost_price ? 'Rs. ' . number_format($price->cost_price, 2) : '-';
            })
            ->addColumn('stock', function ($price) {
                $stock = $price->shopProduct->stocks->where('shop_id', $price->shop_id)->first();
                return $stock ? number_format($stock->quantity, 2) : '0.00';
            })
            // ->addColumn('actions', function ($price) {
            //     return '<button class="btn btn-sm btn-outline-primary" onclick="viewDetails(' . $price->shopProduct->id . ', ' . $price->shop_id . ')">
            //                 <i class="fa fa-eye"></i> View
            //             </button>';
            // })
            // ->rawColumns(['actions'])
            ->make(true);
    }
}
