<?php

namespace App\Http\Controllers;

use App\Enums\DbStatusFieldEnum;
use App\Models\PriceList;
use App\Models\ProfitCenter;
use App\Services\PriceListService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class PriceListController extends Controller
{
    protected $service;

    public function __construct(PriceListService $service)
    {
        $this->service = $service;
    }

    /**
     * Display a listing of the resource.
     */
    public function index(): View|Response|JsonResponse
    {
        if (\request()->input('t') == 1) {
            // ... (keep the dd calls if you want, but for now let's just make it valid)
        }

        return view('modules.priceList.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(): Response|JsonResponse
    {
        return response()->noContent();
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): Response|JsonResponse
    {
        return response()->noContent();
    }

    /**
     * Display the specified resource.
     */
    public function show(int $priceListId): Response|JsonResponse
    {
        return response()->noContent();
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(int $PriceListId): Response|JsonResponse
    {
        return response()->noContent();
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, int $PriceListId): Response|JsonResponse
    {
        return response()->noContent();
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(int $PriceListId): Response|JsonResponse
    {
        return response()->noContent();
    }

    public function getDataTableContent(): JsonResponse
    {
        $ret = ['data' => []];

        $accessSupplierId = ProfitCenter::with('suppliers')->find(auth()->user()->profitCenters->first()->id)->suppliers->pluck('id')->toArray();
        $lastPriceList = [];
        foreach ($accessSupplierId as $supplierId) {
            if ($PriceList = PriceList::where('supplier_id', $supplierId)
                ->where('status', DbStatusFieldEnum::active)
                ->where('available', '<=', Carbon::now()->toDateTimeString())
                ->orderBy('available', 'desc')
                ->first()) {
                $lastPriceList[] = $PriceList->toArray()['id'];
            }
        }
        $s = PriceList::with('supplier', 'attachment')
            ->where('status', DbStatusFieldEnum::active)
            ->whereIn('supplier_id', $accessSupplierId)
            ->where(function ($query) use ($lastPriceList) {
                $query->where('available', '>=', Carbon::now()->toDateTimeString());
                $query->orWhereIn('id', $lastPriceList);
            })
            ->orderBy('available', 'desc');
        $items = $s->get()->toArray();

        foreach ($items as $item) {
            $data['id'] = $item['id'];
            $data['supplierName'] = $item['supplier']['name'];
            $data['available'] = formatterDate($item['available']);
            $data['created_at'] = formatterDateTime($item['created_at']);
            $data['note'] = $item['note'];
            $data['fileUrl'] = false;
            $data['fileName'] = false;
            if ($item['attachment']) {
                $data['fileUrl'] = $item['attachment']['PublicUrl'].$item['attachment']['filename'];
                $data['fileName'] = $item['attachment']['original_filename'];
            }
            $ret['data'][] = $data;

        }

        return \response()->json($ret);

        $ret = ['data' => []];
        if (! $userSuppliers = ProfitCenter::find(auth()->user()->profitCenters()->first()->id)->
        suppliers()->pluck('suppliers.id')) {
            return \response()->json($ret);
        }
        $userSupplierIds = $userSuppliers->toArray();

        $repository = new \App\Repositories\PriceListRepository;
        $repository->setRelation(['supplier', 'attachment']);
        $items = $repository->getQueryWithRelations()->get()->toArray();

        foreach ($items as $item) {
            if (in_array($item['supplier_id'], $userSupplierIds)) {
                $data['id'] = $item['id'];
                $data['supplierName'] = $item['supplier']['name'];
                $data['available'] = formatterDate($item['available']);
                $data['created_at'] = formatterDateTime($item['created_at']);
                $data['fileUrl'] = false;
                $data['fileName'] = false;
                if ($item['attachment']) {
                    $data['fileUrl'] = $item['attachment']['PublicUrl'].$item['attachment']['filename'];
                    $data['fileName'] = $item['attachment']['original_filename'];
                }
                $ret['data'][] = $data;
            }
        }

        return \response()->json($ret);
    }
}
